Rails的关联:我如何限制/范围的has_many:通过与多个自引用的条件是什么? [英] Rails associations: How do I limit/scope a has_many :through with multiple self-referencing conditions?

查看:231
本文介绍了Rails的关联:我如何限制/范围的has_many:通过与多个自引用的条件是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是做到这一点的最好方法是什么?我想这样的事情,但感觉......错了。 (注:这是所有关于:条件中的has_many关联...)

What's the best way to do this? I'm trying something like this, but it feels... wrong. (NOTE: It's all about the :conditions in the has_many association...)

class Submission < ActiveRecord::Base
  belongs_to :checklist
  has_many :jobs, :through => :checklist, :source => :job, :conditions => ["jobs.archived = false OR jobs.archived_at > #{self.created_at} OR jobs.created_at < #{self.created_at}"]
end

Rails的3.2.11和Ruby 1.9.3

Rails 3.2.11, Ruby 1.9.3

我试图做

我需要通过一个的has_many多个条件:通过协会的的参考自我的模式做比较,在这些条件

I need to pass multiple conditions on a has_many :through association and refer to the "self" model to do comparisons in those conditions.

我发现有多个条件的例子。我发现与自我参照比较的例子。但我不能找到这两个元素的东西。这让我觉得我做错了。

I've found examples with multiple conditions. I've found examples with self-referencing comparisons. But I can't find anything with both of those elements. This makes me think I'm doing it wrong.

背景和定义

一个提交记录在时间点的清单及其相关作业的快照。所以,我希望能够说submission.jobs,并查看与提交的清单在当时赞同创建提交作业。*

A Submission records a snapshot of a Checklist and its associated Jobs at a point in time. So, I want to be able to say "submission.jobs" and see the Jobs that were associated with the Submission's Checklist at the time the Submission itself was created.*

*它实际上是稍微复杂:我想创建时的提交和那个还没有被当时归档存在的乔布斯

*It's actually slightly more complicated: I want the Jobs that existed when the Submission was created AND that were not yet archived at that time.

该清单是动态的,可以随时间而改变,但我始终保持历史记录的完整通过一个土生土长的归档系统,所以乔布斯在清单中的提交是为清单创建将是清单的乔布斯在那里工作的时候。 created_at是以前Submission.created_at(且尚未存档)。

The Checklist is dynamic and can change over time, but I always keep historical records intact via a home-grown archiving system, so the Jobs on a Checklist when a Submission was created for that Checklist would be the Checklist's Jobs where Job.created_at was before Submission.created_at (and not yet archived).

所以,虽然提交bel​​ongs_to的一个清单,以及清单的has_many乔布斯,曾经不管特定提交的唯一作业是存在的(并且尚未归档)在提交创建时的作业。

So, although a Submission belongs_to a Checklist, and the Checklist has_many Jobs, the only Jobs that ever matter for a particular Submission are the Jobs that existed (and were not yet archived) at the time the Submission was created.

推荐答案

您可以在工作也许一个方法添加到提交使用范围

You can use a scope on Job and perhaps a method added to Submission:

class Job < ActiveRecord::Base
  scope :unfinished_since, lambda { |timestamp| 
    where("archived = ? OR archived_at > ? OR created_at < ?", false, timestamp, timestamp)        
  }
end

class Submission < ActiveRecord::Base
  belongs_to :checklist
  has_many :jobs, :through => :checklist, :source => :job

  def unfinished_jobs
    jobs.unfinished_since(self.created_at)
  end
end

这篇关于Rails的关联:我如何限制/范围的has_many:通过与多个自引用的条件是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆