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