DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

 

Rails where('n > ?', m) without sql

😅 SQL Way

User.where('age > ?' age)
Enter fullscreen mode Exit fullscreen mode

😆 ORM Way

User.where(User.arel_table[:age].gt(age))
Enter fullscreen mode Exit fullscreen mode

You can also use these methods.

  • = (equal) : eq
  • < (less than) : lt
  • <= (less than equal) : lteq
  • > (greater than) : gt
  • >= (greater than equal) : gteq

Tips

User.where(User.arel_table[:name].matches("%#{name_likely}%"))
Enter fullscreen mode Exit fullscreen mode

💎 Gem Way

I hear that the gem named "Squeel" is very useful for this case.

User.where{age > target_age}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
mculp profile image
Matt Culpepper

Squeel does not seem to be actively maintained.

However, Sequel is another ActiveRecord alternative that gives you that block syntax and is actively maintained.

Sequel is powerful and after using it for the past two years, I think I prefer it to ActiveRecord.

It really makes it hard to do dumb stuff like N+1 queries on accident, but I think it takes a lot longer to understand than ActiveRecord.

We need you.

We're hiring for a Senior Full Stack Engineer and would love for you to apply. Head here to learn more about who we're looking for.