我将关于我的数据库中议会事务的数据保存为Affair
,该数据可以属于Councillor
或Group
。我是Ruby和ActiveRecord的新手。在以前的RoR版本中,我会使用has_and_belongs_to_many
,它也可以使用。
class Councillor < ActiveRecord::Base
has_many :affair_holdings, :foreign_key => :councillor_id
has_many :affairs, through: :affair_holdings
end
class Affair < ActiveRecord::Base
has_many :affair_holdings, :foreign_key => :affair_id
has_many :councillors, through: :affair_holdings
end
class Affair_Holdings < ActiveRecord::Base
belongs_to :affair
belongs_to :councillor
end
稍后在代码中,我想建立一个新的关联:
affair.councillors << Councillor.find_by(:id => 3)
但无论如何它都不起作用。它给我一个错误信息:
/Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/inheritance.rb:125:in `compute_type': uninitialized constant Affair::AffairHolding (NameError)
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/reflection.rb:178:in `klass'
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:123:in `klass'
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/associations/collection_association.rb:37:in `reader'
from /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/associations/builder/association.rb:70:in `affair_holdings'
from crawl.rb:196:in `affair'
from crawl.rb:233:in `<main>'
我的错是什么?我怎么解决这个问题? 谢谢你的帮助。
答案 0 :(得分:3)
班级名称应为AffairHolding
而不是Affair_Holdings
。
答案 1 :(得分:0)
是类名应该是AffairHolding,并且总是使用单数和首字母大写字母作为类名