This is an embarrassing noob question, but...
How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries.
Edit: without using a SQL string!
This is an embarrassing noob question, but...
How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries.
Edit: without using a SQL string!
Just add an OR in the conditions
Model.find(:all, :conditions => ["column = ? OR other_column = ?",value, other_value])
Use ARel
t = Post.arel_table
results = Post.where(
t[:author].eq("Someone").
or(t[:title].matches("%something%"))
)
The resulting SQL:
ree-1.8.7-2010.02 > puts Post.where(t[:author].eq("Someone").or(t[:title].matches("%something%"))).to_sql
SELECT "posts".* FROM "posts" WHERE (("posts"."author" = 'Someone' OR "posts"."title" LIKE '%something%'))