views:

13

answers:

1

How do I find records matching attribute with 1+ values in ActiveRecord/SQL? Examples would be something like:

Post.find_by_type("Post and/or ChildPost")
Post.find(:type => "Post and/or ChildPost")

How can I do this? The number of values will be no more than 10 I'd say.

+2  A: 
Post.find :all, :conditions => ['type IN (?)', ['Post', 'ChildPost']]

Or:

values = ['Post', 'ChildPost']
Post.find :all, :conditions => ['type IN (?)', values]

That should produce the following SQL:

SELECT * FROM `posts` WHERE `type` IN ('Post', 'ChildPost');
aularon
awesome, thanks! how about in just raw sql?
viatropos
No problem :) I edited the answer with expected RAW SQL.
aularon