Rails 3 provides a lot of really neat functionality, and one of the pieces that looked coolest was Arel – ActiveRecord’s own relational algebra. Finally, we could get rid of SQL in queries and use a clear, syntactic DSL to manage our queries!
Well, in reality, that isn’t quite what happened. ActiveRecord’s Arel functionality does provide some neat criteria chaining methods, but unfortunately you either end of typing a lot of raw SQL:
1 2 |
|
Or using unpleasant workarounds to address the underlying Arel for the model:
1
|
|
This is just kinda ugly. Happily, there’s a Gem that addresses this problem called squeel that makes Arel what, in my mind, it should be. It provides an elegant, simple syntax for creating and managing queries that is sensibly divorced both from the underlying Arel and raw SQL of the database:
1
|
|
Much easier to understand! The double-curly braces does kind of suck (this is because it’s a hash inside a proc) but it’s still a fair but more understandable than the default Arel stuff.