Rails 3.2 CRUD :. Where with 'or' conditional

With rubies on the rails, I want to do something like:

@tasks = Task.where(:def => true || :house_id => current_user.house_id) 

What is the most efficient / clean way to do this?

+6
source share
1 answer

You can do it as follows:

 Task.where("def = ? or house_id = ?", true, current_user.house_id) 

General case:

 Model.where("column = ? or other_column = ?", value, other_value) 

You can also use Arel:

 t = Task.arel_table @tasks = Task.where( t[:def].eq(true). or(t[:house_id].eq(current_user.house_id)) ) 
+7
source

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