Ruby on rails 在Rails 3中找不到has_many的反向关联
我有以下型号:Ruby on rails 在Rails 3中找不到has_many的反向关联,ruby-on-rails,ruby,ruby-on-rails-3,associations,Ruby On Rails,Ruby,Ruby On Rails 3,Associations,我有以下型号: class Business < ActiveRecord::Base has_many :customers, :inverse_of => :business has_many :payments, :inverse_of => :business end class Customer < ActiveRecord::Base belongs_to :business, :inverse_of => :customer has_
class Business < ActiveRecord::Base
has_many :customers, :inverse_of => :business
has_many :payments, :inverse_of => :business
end
class Customer < ActiveRecord::Base
belongs_to :business, :inverse_of => :customer
has_many :payments, :inverse_of => :customer
end
class Payment < ActiveRecord::Base
belongs_to :customer, :inverse_of => :payment
belongs_to :business, :inverse_of => :payment
end
class业务:business
有许多:付款,:相反的=>:业务
结束
类Customer:customer
有很多:付款,:相反的=>:客户
结束
类付款:付款相反
属于:业务,与=>:付款相反
结束
做生意。客户工作正常。但是,当我执行业务.payments时,我得到一个错误:找不到业务的反向关联(:payment in business)
我不知道为什么。我两方面都有相同的确切联系。我的schema.db看起来也不错。这里可能有什么问题
编辑
当我删除=>:business
的逆项时,因为有很多:付款
,它就起作用了。为什么会发生这种情况?它是否与付款属于客户和企业有关(这其实并不重要,对吧?您的问题在于:
belongs_to :business, :inverse_of => :customer
及
belongs_to :customer, :inverse_of => :payment
belongs_to :business, :inverse_of => :payment
所属的另一面是有许多
,它定义了复数关系。这意味着
的逆\u应该是客户
而不是客户
和付款
而不是付款
用以下内容更新付款模型:
class Payment < ActiveRecord::Base
belongs_to :customer, :inverse_of => :payments
belongs_to :business, :inverse_of => :payments
end
classpayment:付款相反
属于:业务,与=>:付款相反
结束
你宣布
在商业模式中有许多:付款,与=>:business相反
但在支付中,您使用的属于:业务,:相反的=>:Payment
它应该是属于\u to:business,:inverse\u of=>:payments
您是否应用了适当的rake命令来更新数据库中的关联?@wandarkaf是的,DB:migrate。从我第一次迁移时起,这些关联就存在了。请参见上面的编辑。要点是:参数的倒数必须使用适当的复数。如果它与许多付款相反,请使用:付款,而不是:付款。类似的商业模式