问题描述
在我的申请,我要的型号:工作流程和步骤;步骤 belongs_to的
工作流和工作流的has_many
步骤。步骤有一个索引和一个布尔状态('完成')。
我想要检索的工作流,其完成步骤1和步骤2没有的,即像这样在SQL:
In my application, I have to models: Workflow and Step; steps belongs_to
workflow and a workflow has_many
steps. Steps have an index and a boolean status ('completed').I want to retrieve workflows whose step 1 is completed and step 2 is not, i.e. something like this in SQL:
SELECT * FROM workflows w
INNER JOIN steps s1 ON s1.workflow_id = w.id
INNER JOIN steps s2 ON s2.workflow_id = w.id
WHERE s1.index = 1 AND s1.completed = 1
AND s2.index = 2 AND s2.completed = 0
我试过前preSS此查询与Squeel,但似乎它不允许在同一个协会的多个联接:我无法找到一个方法来命名的连接,当我输入这样的事情: Workflow.joins {步} {.joins步骤}
,生成的SQL很简单:
I've tried to express this query with Squeel, but it seems it does not allow multiple joins on the same association: I cannot find a way to name the joins, and when I type something like this: Workflow.joins{steps}.joins{steps}
, the generated SQL is simply:
SELECT `workflows`.* FROM `workflows`
INNER JOIN `workflow_steps` ON `workflow_steps`.`workflow_id` = `workflows`.`id`
任何想法,我怎么能做到这一点?
Any idea how I can achieve that?
推荐答案
我不知道你会喜欢它,但它通过自我参照是可能的:
I don't know if you gonna like it, but it's possible via self-reference:
Workflow.joins{steps.workfolw.steps}.
where{(steps.index == 1) & (steps.completed == true)} &
(steps.workfolw.steps.index == 2) & (steps.workfolw.steps.completed == false)}.
uniq
这篇关于执行多个联接与Squeel同一协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!