Rails - 使用 has_many 关系和“多”条件进行 activerecord 查找边

发布于 2024-12-01 00:08:28 字数 592 浏览 2 评论 0原文

我是一个完全的业余爱好者,我确信这很容易......但我被困住了。

我有一个简单的情况,我有一个“活动”表,每个活动可以有许多“工作流程”或导致最终完成的状态,我需要在控制器中执行活动记录查找,以便我最终得到活动没有任何状态字段包含“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳 2024-12-08 00:08:28

您是否尝试过使用.to_sql查看生成的sql语句?

我想以下应该可以工作:(

Activity.all(:include => :workflows, :joins => :workflows, :conditions => "workflows.status != COMPLETED")

您正在使用rails 2 finder语法,所以我猜您没有使用rails 3,对吧?)

======更新======

Activity.all(:include => :workflows, :conditions => "workflows.status != 'COMPLETED'")

似乎rails 2会做一个:include 的外部联接,因此不再需要使用 :joins

另外,COMPLETED 应该用 ' 引用。

希望这对你有用!

Have you tried to see the generated sql statement by using .to_sql?

I guess the following should work:

Activity.all(:include => :workflows, :joins => :workflows, :conditions => "workflows.status != COMPLETED")

(You are using rails 2 finder syntax, so I guess you are not using rails 3, right?)

====== UPDATED ======

Activity.all(:include => :workflows, :conditions => "workflows.status != 'COMPLETED'")

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文