Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

Tips and tricks

ernie edited this page May 10, 2012 · 6 revisions

Squeel Tips and Tricks

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.

Arbitrary number of AND/ORed conditions against a single column

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.

Use __send__ to dynamically reference columns/associations

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
Clone this wiki locally