Is there a AR#or_where
in Rails 3 as in
User.where(cond1).or_where(cond2)
?
There isn't a method like that in Rails.
I think that the easiest way to do an OR in a WHERE clause is to do something like this:
User.where("first_name = ? OR last_name = ?", params[:first_name], "Wilson")
If you want to learn the Arel methods (which does have an "or" method), look here: https://github.com/rails/arel#readme. Using Arel and ActiveRecord, you can do the same thing like this:
User.where(User.arel_table[:first_name].eq(params[:first_name]).or(User.arel_table[:last_name].eq('Wilson')))
You can take a look at meta_where. This gem allows more powerful ActiveRecord query expressions in Ruby code, without requiring dropping to SQL fragments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With