Question How can I use arel in Rails to perform a simple OR clause

 
  • Created:over 2 years ago
  • Status:resolved

I have a table:

User(name, age) 

I would like to select all the users that are less than 18 years old or older than 80.

I know that I can achieve this the old way using the following query:

User.where("age < ? or age > ?", 20, 80).to_a

But I would like to build the expression dynamically, so I'm interested in how I can do this using arel.

 

1 Answers

votes newest oldest
 
  • Created:over 2 years ago
accepted answer

Try the following:

t = User.arel_table
User.where(t[:age].lt(18).or(t[:age].gt(80)).to_a