rails joins query over has_many objects independently
我有父模型
一个项目有许多
我想搜索在该时间范围内有"任何"待办事项的项目。
在这里我写了一些代码,但是,这与预期的不一样。
1 2 3 4 5 6 7 | class Project has_many :todos scope :active, -> {joins(:todos).where("todos.starts_at < '#{Time.now}' AND todos.ends_at > '#{Time.now}'").distinct} scope :waiting, -> {joins(:todos).where.not("todos.starts_at < '#{Time.now}' AND todos.ends_at > '#{Time.now}'").distinct} scope :done, -> {where("project_due > ?", Time.now)} end |
主动活动似乎是正确的,但是等待范围还包含具有多个待办事项的项目。
我想知道是否有解决方案可以比较每个待办事项的
谢谢你
*#更新*
这就是我想要实现的。 但是在一个查询中
1 | scope :waiting, -> { joins(:todos).where.not(id: active.ids).where('finishing > ?', Time.now).distinct } |
您的等待查询基本上从
另外,为了获得最佳的Rails做法,您应该使用问号编写查询,如其他答案中建议的用户fool-dev一样。这是为了防止SQL注入,如此处详细说明
编辑
因此,我认为您的意思是等待是指没有任何待办事项或等待待办事项(
1 | scope(:waiting) { includes(:todos).where('todos.project_id IS NULL OR todos.start_at > ?', Time.now).distinct } |
第一部分是选择没有任何待办事项的项目,第二部分是不言自明的。
尝试以下
更新资料
等待是指
1 | scope :waiting, -> {joins(:todos).where("todos.starts_at >= ?", Time.now).distinct} |
如果与第一个条件匹配,那么您就不需要与第二个条件匹配,则可以使用第二个条件进行编写,如下所示
1 | scope :waiting, -> {joins(:todos).where("todos.starts_at > '#{Time.now}' AND todos.ends_at > '#{Time.now}'").distinct} |
但不需要
更新2
从
1 | scope :waiting, -> {joins(:todos).where("todos.starts_at >= ?", Time.now).distinct} |
更新2生效后的更新3
1 2 3 | scope :waiting, -> {joins(:todos).where("todos.starts_at >= ?", Time.now).distinct} scope :finished, -> {where("finishing > ?", Time.now).distinct} scope :waiting_n_progress, -> {where.not(id: active.ids).finished.waiting} |
希望它能工作。