overlox
overlox

Reputation: 822

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?

Upvotes: 6

Views: 1176

Answers (1)

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56595

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))
)

Upvotes: 7

Related Questions