创建第三级关联,其中第二级属于 Rails ActiveRecord 上的第三级
我的 Rails 应用程序上有以下模型“购买”:
class Purchase < ActiveRecord::Base
[...]
belongs_to :payment, :validate => true
belongs_to :day, :foreign_key => :day_day, :primary_key => :day,
:counter_cache => true
[...]
end
我有“日”模型:
class Day < ActiveRecord::Base
[...]
has_many :purchases, :foreign_key => :day_day, :primary_key => :day
[...]
end
我想通过“购买”模型在当天和当天发生的付款之间创建关联。是否可以?
多谢!
I have the following model, Purchase, on my Rails app:
class Purchase < ActiveRecord::Base
[...]
belongs_to :payment, :validate => true
belongs_to :day, :foreign_key => :day_day, :primary_key => :day,
:counter_cache => true
[...]
end
And I have the Day model:
class Day < ActiveRecord::Base
[...]
has_many :purchases, :foreign_key => :day_day, :primary_key => :day
[...]
end
I'd like to create an association between the day and the payments occurred during that day through the Purchase model. Is it possible?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您能更详细地说明为什么你们的关系是这样建立的,可能会有所帮助。
我的第一个问题是 Day 对象由什么组成?能否用
Purchase
模型上的“purchased_on”属性来替换它?如果是这样,可以通过
Purchase
上的范围轻松完成此操作。其次,您现在的方式是,一次
付款
可以包含许多购买
。这确实是您的意思吗,还是您实际上想要一个包含许多付款
的购买
?请告诉我,我看看能否为您解决问题。
It might help if you can give a little more detail on why your relationships are set up the way they are.
My first question is what does a
Day
object consist of? Could it be replaced by having a "purchased_on" attribute on thePurchase
model?If so, this can be done pretty easily through a scope on
Purchase
.Second, the way you have it now a single
Payment
has manyPurchases
. Is this really what you mean, or do you actually want aPurchase
that has manyPayments
?Let me know, and I'll see if we can get something worked out for you.