Rails - 使用 has_many 关系和“多”条件进行 activerecord 查找边
我是一个完全的业余爱好者,我确信这很容易......但我被困住了。
我有一个简单的情况,我有一个“活动”表,每个活动可以有许多“工作流程”或导致最终完成的状态,我需要在控制器中执行活动记录查找,以便我最终得到活动没有任何状态字段包含“COMPLETED”的工作流程
class Activity < ActiveRecord::Base
has_many :workflows
class Workflow < ActiveRecord::Base
belongs_to :activity
在控制器中:
Activity.all(:include => :workflows, :conditions => workflows.status != 'COMPLETED')
这在一定程度上起作用,但不是只给我没有任何状态为“COMPLETED”的工作流程的活动,我得到的结果是包括具有已完成工作流程的活动,只要该活动还具有其他未完成工作流程...希望这是有意义的。
最重要的是,我如何取回其工作流程的活动,无论有多少,都不包含状态“已完成:?
谢谢!
I'm a complete amateur and I'm sure this is easy...but I'm stuck.
I have a simple situation, I have a table of "activities", each activity can have many "workflows" or statuses leading up to an eventual completion, I need to perform an activerecord find in a controller such that I end up with the activities that do not have any workflows with a status field containing "COMPLETED"
class Activity < ActiveRecord::Base
has_many :workflows
class Workflow < ActiveRecord::Base
belongs_to :activity
In the controller:
Activity.all(:include => :workflows, :conditions => workflows.status != 'COMPLETED')
This is working to a degree, but instead of giving me only activites that do not have any workflows with status "COMPLETED", I'm getting results that include activites with a COMPLETED workflow as long as that activity also has other, non-COMPLETED workflows...hope this makes sense.
Bottom line, how can I get back the activities whose workflows, no matter how many there may be, do not include status "COMPLETED:?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用
.to_sql
查看生成的sql语句?我想以下应该可以工作:(
您正在使用rails 2 finder语法,所以我猜您没有使用rails 3,对吧?)
======更新======
似乎rails 2会做一个
:include
的外部联接,因此不再需要使用:joins
。另外,
COMPLETED
应该用'
引用。希望这对你有用!
Have you tried to see the generated sql statement by using
.to_sql
?I guess the following should work:
(You are using rails 2 finder syntax, so I guess you are not using rails 3, right?)
====== UPDATED ======
It seems like rails 2 will do a outer join for
:include
, so no need to use:joins
anymore.Also, the
COMPLETED
should be quoted by'
.Hope this works for you!