I'm sure there's an easy answer for this, but not sure how to word it to search for it.
Given that articles belong to users, if I have a set of three different users, how can I access all of the articles written by any of those users with one query?
I'm sure there's an easy answer for this, but not sure how to word it to search for it.
Given that articles belong to users, if I have a set of three different users, how can I access all of the articles written by any of those users with one query?
Given that user_ids is an array of the user ids:
Article.all(:joins => :users, :conditions => ["users.id in ?", user_ids])
hellvinz's answer helped me work it out :) Since the articles belong to users, that means that they have the user_id attribute on them. I feel pretty silly about this one now xD
user_ids = users.map(&:id)
articles = Article.all :conditions => {:user_id => user_ids}
Saves a join, producing a simpler query.
Thanks!