Reputation: 11
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!
Upvotes: 1
Views: 3326
Reputation: 16011
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!
Upvotes: 3