Sean H. wrote:
I have a Department model that has_many people. How do I go about
finding all departments with more than 0 people?
Department.find(:all, :conditions => Department.people.size > 0)
That, of course, doesn’t work, but it was as close as I could come.
Assuming that you actually need to display info about the people in each
department, the easiest way to do this is probably:
Department.find(:all, :include => :people,
:conditions => “people.department_id is not NULL”)
If you don’t need to return the data from the people association, but
to find all the departments with people in it, you can do:
Department.find(:all, :conditions => “people.department_id is not NULL”,
:joins => “left join people on people.department_id =
Haven’t tested that one, but it should do the same as the above, but not
back any columns from the people table.