Ruby on Rails 3 howto make 'OR' condition
Solution 1:
Account.where(id: [1,2])
no explanation needed.
Solution 2:
This will works in Rails 5, see rails master :
Post.where('id = 1').or(Post.where('id = 2'))
# => SELECT * FROM posts WHERE (id = 1) OR (id = 2)
For Rails 3.0.4+:
accounts = Account.arel_table
Account.where(accounts[:id].eq(1).or(accounts[:id].eq(2)))
Solution 3:
Those arel queries are unreadable to me.
What's wrong with a SQL string? In fact, the Rails guides exposes this way as the first way to make conditions in queries: http://guides.rubyonrails.org/active_record_querying.html#array-conditions
So, I bet for this way to do it as the "Rails way":
Account.where("id = 1 OR id = 2")
In my humble opinion, it's shorter and clearer.
Solution 4:
Sadly, the .or isn't implemented yet (but when it is, it'll be AWESOME).
So you'll have to do something like:
class Project < ActiveRecord::Base
scope :sufficient_data, :conditions=>['ratio_story_completion != 0 OR ratio_differential != 0']
scope :profitable, :conditions=>['profit > 0']
That way you can still be awesome and do:
Project.sufficient_data.profitable
Solution 5:
I'd go with the IN
clause, e.g:
Account.where(["id in (?)", [1, 2]])