gpt4 book ai didi

ruby-on-rails - 无法在 Rails 3 中找到 has_many 的逆关联

转载 作者:数据小太阳 更新时间:2023-10-29 06:40:39 25 4
gpt4 key购买 nike

我有以下模型:

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 :paymentsinverse_of => :business 时,它起作用了。为什么会这样?这与付款属于客户和企业有关吗(应该无关紧要,对吧?)?

最佳答案

用这个更新支付模型:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com