我有以下型号:

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/

10-14 01:42