lundi 16 mars 2015

How to select the items that have an association with any user in Rails

I have a relation between User and Hobby, the relation in User is has_and_belongs_to_many and the one for Hobby is also has_and_belongs_to_many.


I'm trying to get the list of Hobbies that Users have chosen. For example if one user selected Soccer and another selected Basketball, I want to have a query that shows me "Soccer, Basketball". How can I do this join in a query? Right now I'm grabbing the hobbies for each User via User.find(id).hobbies


Aucun commentaire:

Enregistrer un commentaire