views:

68

answers:

1

I'm having a problem coming up with a query that would work for these models. I have three models that are connected.

An Organization has many Users, and Users have many StatusEntries

Basically this means I could do

Organization.find(1).users.find(1).status_entries

And have a list of status_entries returned to me.

The problem is I am trying to find a list of status_entries for a specific Organization. I am having trouble coming up with a way of doing this that is not overly complicated and is elegant. Any help would be much appreciated.

+10  A: 

I think it's a case for a have_many :through association :

class Organization
  has_many :users
  has_many :status_entries, :through => :users
end

then you can do :

Organization.find(1).status_entries
tal
Awesome response, it did not even occur to me to do that. Cuts out like 15 lines of cruft from different places. Thank you!
ohdeargod