ruby-on-rails
  • ruby-on-rails-4
  • has-and-belongs-to-many
  • model-associations
  • refinerycms
  • 2016-08-27 20 views 0 likes 
    0

    我已經在煉油廠CMS中創建一個HABTM關聯,但它返回錯誤象下面這樣:如何插入數據導軌4

    ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "refinery_georgiantoast_restaurant_wines" does not exist 
    LINE 5:    WHERE a.attrelid = '"refinery_georgiantoast_r... 
                 ^
    :    SELECT a.attname, format_type(a.atttypid, a.atttypmod), 
            pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod 
           FROM pg_attribute a LEFT JOIN pg_attrdef d 
            ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
           WHERE a.attrelid = '"refinery_georgiantoast_restaurant_wines"'::regclass 
           AND a.attnum > 0 AND NOT a.attisdropped 
           ORDER BY a.attnum 
    

    /供應商/擴展/ georgiantoasts /應用/型號/refinery/georgiantoast/restaurant.rb

    這裏是我的restaurant.rb型號:

    module Refinery 
        module Georgiantoast 
        class Restaurant < Refinery::Core::BaseModel 
    
         validates :title, :presence => true, :uniqueness => true 
    
         belongs_to :avatars, :class_name => '::Refinery::Image' 
    
         has_many :restaurants_wines 
    
         has_many :wines, :through => :restaurants_wines 
    
         # To enable admin searching, add acts_as_indexed on searchable fields, for example: 
         # 
         # acts_as_indexed :fields => [:title] 
    
        end 
        end 
    end 
    

    /vendor/extensions/georgiantoasts/app/models/refinery/georgiantoast/wine.rb

    這裏是我的wine.rb型號:

    module Refinery 
        module Georgiantoast 
        class Wine < Refinery::Core::BaseModel 
    
         validates :title, :presence => true, :uniqueness => true 
    
         belongs_to :avatars, :class_name => '::Refinery::Image' 
    
         has_many :restaurants_wines 
    
         has_many :restaurants, :through => :restaurants_wines 
    
         # To enable admin searching, add acts_as_indexed on searchable fields, for example: 
         # 
         # acts_as_indexed :fields => [:title] 
    
        end 
        end 
    end 
    

    /供應商/擴展/ georgiantoasts /應用程序/模型/煉油廠/ georgiantoast/restaurant_wines.rb

    這裏是我的連接表模型restaurant_wines.rb

    module Refinery 
        module Georgiantoast 
        class RestaurantWine < Refinery::Core::BaseModel 
    
         belongs_to :wine 
    
         belongs_to :restaurant 
    
         # To enable admin searching, add acts_as_indexed on searchable fields, for example: 
         # 
         # acts_as_indexed :fields => [:title] 
    
        end 
        end 
    end 
    

    /vendor/extensions/georgiantoasts/app/controllers/refinery/georgiantoast/admin/restaurants_controller.rb

    這裏是restaurants_controller.rb控制器:

    module Refinery 
        module Georgiantoast 
        module Admin 
         class RestaurantsController < ::Refinery::AdminController 
    
         crudify :'refinery/georgiantoast/restaurant' 
    
         private 
    
         # Only allow a trusted parameter "white list" through. 
         def restaurant_params 
          params.require(:restaurant).permit(:title, :description, :address, :town, :state, :lat, :lng, :avatars_id, :wine_ids => []) 
         end 
         end 
        end 
        end 
    end 
    

    /vendor/extensions/georgiantoasts/app/views/refinery/georgiantoast/admin/restaurants/_form.html.erb

    這裏是餐廳_form.html.erb形式:

    <%= form_for [refinery, :georgiantoast_admin, @restaurant] do |f| -%> 
        <%= render '/refinery/admin/error_messages', 
           :object => @restaurant, 
           :include_object_name => true %> 
    
        <div class='field'> 
         <%= f.label :title -%> 
         <%= f.text_field :title, :class => 'larger widest' -%> 
        </div> 
    
        <div class='field'> 
         <%= render '/refinery/admin/wysiwyg', 
           :f => f, 
           :fields => [:description], 
           :object => "georgiantoast/restaurant" -%> 
        </div> 
    
        <div class='field'> 
         <%= f.label :address -%> 
         <%= f.text_field :address -%> 
        </div> 
    
        <div class='field'> 
         <%= f.label :wine -%> 
         <%= select_tag "#{f.object_name}[wine_ids][]", options_for_select(Refinery::Georgiantoast::Wine.all.collect { |wine| [wine.title, wine.id] }), {multiple: true} -%> 
        </div> 
    
    
        <%= render '/refinery/admin/form_actions', :f => f, 
           :continue_editing => false, 
           :delete_title => t('delete', :scope => 'refinery.restaurants.admin.restaurants.restaurant'), 
           :delete_confirmation => t('message', :scope => 'refinery.admin.delete', :title => @restaurant.title) -%> 
    <% end -%> 
    

    下面是遷移文件:

    class CreateJoinTableRestaurantsWines < ActiveRecord::Migration 
        def change 
        create_join_table :restaurants, :wines do |t| 
         t.index :restaurant_id 
         t.index :wine_id 
        end 
        end 
    end 
    

    當我發表我的形式,它沒有執行調用連接表的任何行動。

    如果我嘗試下面的命令上rails console運行它返回我以下錯誤:

    2.2.3 :001 > r = Refinery::Georgiantoast::Restaurant.first 
        Refinery::Georgiantoast::Restaurant Load (1.0ms) SELECT "refinery_georgiantoast_restaurants".* FROM "refinery_georgiantoast_restaurants" ORDER BY "refinery_georgiantoast_restaurants"."id" ASC LIMIT 1 
    => #<Refinery::Georgiantoast::Restaurant id: 1, title: "abc restaurant", description: "<p>abc restaurant</p>", address: "12 helifax estate", town: "helifax", state: "toronto", lat: #<BigDecimal:55fdfc0,'-0.12555555E2',18(27)>, lng: #<BigDecimal:55fde80,'0.74555555E2',18(27)>, avatars_id: 3, position: nil, created_at: "2016-08-27 11:26:29", updated_at: "2016-08-27 11:26:29"> 
    2.2.3 :002 > r.wine_ids 
    ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "refinery_georgiantoast_restaurant_wines" does not exist 
    LINE 5:    WHERE a.attrelid = '"refinery_georgiantoast_r... 
                 ^
    :    SELECT a.attname, format_type(a.atttypid, a.atttypmod), 
            pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod 
           FROM pg_attribute a LEFT JOIN pg_attrdef d 
            ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
           WHERE a.attrelid = '"refinery_georgiantoast_restaurant_wines"'::regclass 
           AND a.attnum > 0 AND NOT a.attisdropped 
           ORDER BY a.attnum 
    
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:733:in `column_definitions' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/schema_statements.rb:197:in `columns' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/connection_adapters/schema_cache.rb:49:in `columns_hash' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:85:in `column_for' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:94:in `bind' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:103:in `last_chain_scope' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:139:in `add_constraints' 
    ... 9 levels... 
        from /home/muhammad/.rvm/gems/[email protected]/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require' 
        from /home/muhammad/workspace/georgian-toast-wines/bin/rails:9:in `<top (required)>' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency' 
        from /home/muhammad/.rvm/gems/[email protected]/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load' 
        from /home/muhammad/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
        from /home/muhammad/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
        from -e:1:in `<main>' 
    

    所有遷移已經跑了,不遷移或種子掛起了。

    回答

    0

    如果你使用的form_for你可以使用這個波紋管在視圖應用/視圖/餐廳/ _form

    <%= f.collection_select :wine_ids, @wines, :id, :name, {}, {:multiple => true} %> 
    

    不要在您的控制器忘了這

    params.require(:restaurant).permit(:name, :wine_ids => []) 
    
    +0

    它說:'未初始化的精煉廠:: Georgiantoast ::餐廳:: RestaurantsWine' 也是這是我的餐館控制器新方法'def new @wines = Refinery :: Georgiantoast: :Wine.all end'我在餐廳控制器中寫入了新方法來訪問'@ wines' – LearningROR

    +0

    在rails控制檯中它工作嗎? 'Restaurant.first.wines.new' –

    +0

    我試過了:'Refinery :: Georgiantoast :: Restaurant.first.wines.new'它說:'NameError:未初始化的精煉廠:: Georgiantoast :: Restaurant :: RestaurantsWine' – LearningROR

    0

    你似乎有一個葡萄酒和餐廳班的錯字。這種關係應該通過restaurant_wines而不是restaurants_wines(所以只有「酒」應該是複數)。

    如果您還沒有這樣做,則需要將wine_idrestaurant_id列添加到連接表中。一旦他們在那裏,你應該是

    +0

    非常感謝很多用於識別,以及我如何在rails控制檯中訪問它?是的,我在名爲「restaurants_wines」的連接表中添加了'wine_id'和'restaurant_id',表名很好'restaurants_wines'? – LearningROR

    +0

    我試過了:'煉油廠::Gorgiantoast :: Restaurant.first.wines.new'它說:'NameError:未初始化的精煉廠::Godiantoast :: Restaurant :: RestaurantsWine' – LearningROR

    +0

    看起來你仍然指的是不正確的拼寫在您的應用程序的某處。你是否已經改變了餐廳模型中has_many關係中'restaurant_wines'的拼寫?如果是這樣,你有沒有嘗試重新啓動rails控制檯或服務器?除此之外,我認爲你試圖創造記錄的方式應該是有效的。 – kohrVid

    相關問題