Over a million developers have joined DZone.

On ActiveRecord Query Enhancers

DZone 's Guide to

On ActiveRecord Query Enhancers

· Big Data Zone ·
Free Resource

The question is: Should we use the third-party ActiveRecord Query Enhancers, such as SearchLogic, Squeel or MetaSearch?

Quoting from Squeel’s Github README page:

Squeel lets you rewrite...

Article.where ['created_at >= ?', 2.weeks.ago]

Article.where{created_at >= 2.weeks.ago}
This is a good thing. If you don't agree, Squeel might not be for you.

At work, we are migrating a Rails 3.0 project into Rails 3.2. We used MetaSearch in the project quite extensively, and we're now discussing whether using Squeel (successor of MetaSearch for newer Rails versions) would be a good decision. I recognized there are good points on both sides of the debate and wanted to capture the list here for a reference.

You’d Want to Use Such Enhancers Because:

  1. They extend the basic AR API to provide a DSL. For example, Squeel provides an API so you can write the following: User.where{ country != "USA" && drives_truck == true } instead of this ActiveRecord Query: User.where('country <> ? && drives_truck = ? ', 'USA', true).
  2. They write complex join statements, including outer joins and joining multiple tables, using a shorthand. For example: User.where{company_name_eq 'Coders'}.
  3. They support negative logic (not equal, not in) and OR SQL queries that would require raw String queries using the ActiveRecord API.
  4. They provide fancy operations such as User.where{name_or_address_contains 'scott'} that would require some raw String when using AR directly.

You’d Avoid Using These Enhancers Because:

  1. You think that using a String is just fine over using a Hash with hardcoded symbols.
  2. As new versions of AR are released, there’s little guarantee the third-party API will still be compatible.
  3. You are concerned about adding another pile of abstractions and magic on top of ActiveRecord.

Please share if you prefer one over another, and if you do, please let us know why.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}