创建第三级关联,其中第二级属于 Rails ActiveRecord 上的第三级

发布于 2024-09-26 13:11:40 字数 491 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-10-03 13:11:40

如果您能更详细地说明为什么你们的关系是这样建立的,可能会有所帮助。

我的第一个问题是 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 the Purchase 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 many Purchases. Is this really what you mean, or do you actually want a Purchase that has many Payments?

Let me know, and I'll see if we can get something worked out for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文