无法在Rails 3中找到has_many的反向关联

dar*_*sky 12 ruby ruby-on-rails associations ruby-on-rails-3

我有以下型号:

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
Run Code Online (Sandbox Code Playgroud)

做得business.customers很好.但是,当我这样做时,business.payments我收到一个错误:Could not find the inverse association for business (:payment in Business).

我不知道为什么会这样.我两种方式都有完全相同的关联.我的schema.db也很好看.这可能是什么问题?

编辑 当我删除inverse_of => :businessfor has_many :payments,它的工作原理.为什么会这样?是否与付款属于客户和业务有关(这应该不重要,对吧?)?

Mun*_*sim 26

使用此更新付款模式:

class Payment < ActiveRecord::Base
  belongs_to :customer, :inverse_of => :payments 
  belongs_to :business, :inverse_of => :payments
end
Run Code Online (Sandbox Code Playgroud)

你声明了

has_many :payments, :inverse_of => :business 在商业模式中

但在付款时你用过 belongs_to :business, :inverse_of => :payment

它应该是 belongs_to :business, :inverse_of => :payments