命名范围与 find_by_sql (具体示例)
出于好奇,有人知道使用命名范围(而不是 find_by_sql )构建以下集合的更好方法吗?
@available = Workflow.find_by_sql(["
SELECT workflows.id FROM workflows
WHERE workflows.project_id = ? AND workflows.status < 5 AND
( workflows.created_by = ? OR workflows.id IN
(
SELECT workflow_id FROM workflow_histories
INNER JOIN workflow_recipients on workflow_histories.id = workflow_recipients.workflow_history_id
WHERE workflow_recipients.recipient_id = ? AND workflow_recipients.recipient_type = ?
)
)", project.id, @current_user.id, @current_user.id , 'USER'])
Just out of curiosity, does anyone know a better way of building the following collection using named scopes (as opposed to find_by_sql)?
@available = Workflow.find_by_sql(["
SELECT workflows.id FROM workflows
WHERE workflows.project_id = ? AND workflows.status < 5 AND
( workflows.created_by = ? OR workflows.id IN
(
SELECT workflow_id FROM workflow_histories
INNER JOIN workflow_recipients on workflow_histories.id = workflow_recipients.workflow_history_id
WHERE workflow_recipients.recipient_id = ? AND workflow_recipients.recipient_type = ?
)
)", project.id, @current_user.id, @current_user.id , 'USER'])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有测试过这个,但我认为它会起作用:(
我的答案的先前版本将子选择分解为它自己的查询,我认为这是不必要的。)
I haven't tested this, but I think it would work:
(A previous version of my answer breaks the sub-select out into its own query, which I think is unnecessary.)