Tutorial :I need help figuring out the best way to query this in rails



Question:

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.


Solution:1

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  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »