Rails associations: How do I limit/scope a has_many :through with multiple self-referencing conditions?

这一生的挚爱 提交于 2019-12-04 19:40:56

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!