我有以下型号:
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
做
business.customers
很好。但是,当我business.payments
时,我会得到一个错误:Could not find the inverse association for business (:payment in Business)
。但我不知道为什么。我对这两种方式有着完全相同的联想。我的schema.db看起来也不错。有什么问题吗?
编辑
当我为
inverse_of => :business
移除has_many :payments
时,它会工作。为什么会这样?是否与客户和企业的付款有关(这应该不重要,对吧?)? 最佳答案
使用以下内容更新付款模式:
class Payment < ActiveRecord::Base
belongs_to :customer, :inverse_of => :payments
belongs_to :business, :inverse_of => :payments
end
你宣布
has_many :payments, :inverse_of => :business
商业模式但在付款时,您使用了
belongs_to :business, :inverse_of => :payment
应该
belongs_to :business, :inverse_of => :payments
关于ruby-on-rails - 在Rails 3中找不到has_many的逆关联,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17233483/