I have a simple question – basically I want to fetch all ActiveRecords of some model X which adhere to some conditions. I am trying to use the X.where method here but I am not sure how it will work. Basically, my model X has_many Y.
I have a list of ids for model Y objects. I want to find all of model X which has at least one of those ids in its has_many Y.
Is there a simple way I can do this using X.where? Or do i need more complicated sql?
Here is what I would do:
Another solution that I prefer is to use scopes:
then you can do stuff like:
or chaining: (just rememeber to call all when you actually want to run the query)