Answer updated (twice) to reflect Rails 3+ ActiveRecord syntax
Rails 3+
You can use the following syntax:
Post.where(user_id: [1,2,3])
And thus:
Post.where(user_id: args)
Rails 3 or Rails 4 (Original)
For completeness, the Rails 3 syntax would be:
Post.where("user_id IN (?)", [1,2,3]).to_a
If args is an Array
, then:
Post.where("user_id IN (?)", args).to_a
Or, if args is an array of strings:
Post.where("user_id IN (?)", args.map(&:to_i)).to_a
The to_a
is optional, but will return the results as an array. Hope this helps.
Rails 2
You should be able to simply pass in an array as the parameter, as follows:
find(:all, :conditions => ["user_id IN (?)", [1,2,3] ] )
Thus, if args is an array of ids, you should simply call:
find(:all, :conditions => ["user_id IN (?)", args ] )
In case you have an issues with types, you can do:
find(:all, :conditions => ["user_id IN (?)", args.map { |v| v.to_i } ] )
and this will ensure each item in the array is an Integer.