This repository was archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 210
Tips and tricks
ernie edited this page May 10, 2012
·
6 revisions
Have a handy pattern you've adopted that showcases how Squeel can make your code more readable, or allow you to do things that you couldn't easily do with SQL strings? Put it here. To start things off, a sample from the README.
In standard ActiveRecord, to handle an arbitrary list of potential matches on a single database column, you might do:
Person.where((['name LIKE ?'] * names.size).join(' OR '), *names)With Squeel, you can use the *_any predicates:
Person.where{name.like_any names}For AND, just use the *_all predicates, instead.
Squeel leaves a few standard Object methods available inside its DSL, and one of them is __send__. It's useful because you can create Stubs dynamically. For example, you can dynamically create conditions against lists of columns:
def self.containing_term_in_any_columns(term, *search_columns)
where{search_columns.map {|col| __send__(col).matches "%#{term}%"}.inject(&:|)}
end