GhostRider GhostRider - 1 year ago 101
SQL Question

Rails: Getting all has_many objects associated with an active relation of parents

I have what I suspect is a basic question. In my

app, One
has many
. Is there a way to get all the scores of an active relation of users. I can do user.scores (obviously) but if users is a group of users I need to do something like
. This is clearly not correct.

Answer Source

You can use

Score.where(user: users)

This will construct sql like

select * from scores where user_id in (select *.id from users where ..) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download