Rails 3: Is there an "AR # or_where"?

Is there AR#or_where in Rails 3, as in

 User.where(cond1).or_where(cond2) 

?

+4
source share
4 answers

There is no such method in Rails.

I think the easiest way to do 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 about Arel methods (which has an β€œor” method), look here: https://github.com/rails/arel#readme . Using Arel and ActiveRecord, you can do the same:

 User.where(User.arel_table[:first_name].eq(params[:first_name]).or(User.arel_table[:last_name].eq('Wilson'))) 
+4
source

You can look at meta_where . This pearl allows you to use the more powerful ActiveRecord query expressions in Ruby code without requiring you to drop SQL fragments.

+2
source

Just using User.where(cond1) | User.where(cond2) User.where(cond1) | User.where(cond2) , || reserved by ruby!

+2
source

If you want to link them like this, you need to dig into isl a bit. See this similar SO question .

0
source

Source: https://habr.com/ru/post/1332509/


All Articles