DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

3 2

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.

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay