UnknownAttributeError:Control :: Comment的未知属性'news_id'

时间:2016-11-16 07:01:14

标签: ruby-on-rails ruby foreign-keys

尝试在新闻中发表评论时,我收到名称空间模型的未知属性错误。

控制/ news.rb

class Control::News < ApplicationRecord
    belongs_to :user
    belongs_to :control_category, class_name: "Control::Category"
    has_many :control_comments, class_name: "Control::Comment", :foreign_key => 'control_news_id'
end

控制/ comments.rb

class Control::Comment < ApplicationRecord
    belongs_to :control_news, class_name: "Control::News", :foreign_key => "control_news_id"
end

评论迁移文件

class CreateControlComments < ActiveRecord::Migration[5.0]
  def up
    create_table :control_comments do |t|
      t.string "name"
      t.string "email"
      t.text "comment"
      t.references :control_news, foreign_key: true
      t.boolean "status", :default => false
      t.timestamps
    end
    def down
        drop_table :comments
    end
  end
end

它不起作用。每当我尝试发表评论时,我都会获得未知属性“news_id”,但外键是“control_news_id”。请帮我解决。

Rails控制台

a = Control::News.find(112)
b = b.control_comments.create(name: "test")

ActiveModel::UnknownAttributeError: unknown attribute 'news_id' for Control::Comment.
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activemodel-5.0.0.1/lib/active_model/attribute_assignment.rb:48:in `_assign_attribute'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activemodel-5.0.0.1/lib/active_model/attribute_assignment.rb:40:in `block in _assign_attributes'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activemodel-5.0.0.1/lib/active_model/attribute_assignment.rb:39:in `each'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activemodel-5.0.0.1/lib/active_model/attribute_assignment.rb:39:in `_assign_attributes'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/attribute_assignment.rb:26:in `_assign_attributes'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activemodel-5.0.0.1/lib/active_model/attribute_assignment.rb:33:in `assign_attributes'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/associations/association.rb:172:in `initialize_attributes'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/associations/association.rb:254:in `block in build_record'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/core.rb:321:in `initialize'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/inheritance.rb:65:in `new'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/inheritance.rb:65:in `new'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/reflection.rb:152:in `build_association'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/associations/association.rb:253:in `build_record'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/associations/collection_association.rb:165:in `build'
    from /Users/santosharyal/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.1/lib/active_record/associations/collection_proxy.rb:294:in `build'
    from (irb):7

1 个答案:

答案 0 :(得分:1)

由于您是手动设置class_name,因此Active Record无法自动识别双向关系。您需要通过添加inverse_of告诉Control :: News如何找到自己。

$arg

您可以在此处详细了解双向关联http://guides.rubyonrails.org/association_basics.html#bi-directional-associations