【发布时间】:2013-06-21 10:54:06
【问题描述】:
我有以下型号:
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 看起来也不错。这可能是什么问题?
编辑
当我删除has_many :payments 的inverse_of => :business 时,它可以工作。为什么会这样?是否与付款属于客户和企业有关(这应该不重要,对吧?)?
【问题讨论】:
-
您是否应用了正确的 rake 命令来更新数据库中的关联?
-
@wandarkaf 是的,db:migrate。当我第一次迁移时,这些关联就在那里。请参阅上面的编辑。
标签: ruby-on-rails ruby ruby-on-rails-3 associations