Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2 CRUD : .where with 'or' conditional

With ruby on 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?

like image 875
overlox Avatar asked Oct 13 '12 15:10

overlox


1 Answers

You can do it like this:

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

The general case is:

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

You can also leverage Arel:

t = Task.arel_table

@tasks = Task.where(
  t[:def].eq(true).
  or(t[:house_id].eq(current_user.house_id))
)
like image 59
Bozhidar Batsov Avatar answered Oct 09 '22 18:10

Bozhidar Batsov