Over a million developers have joined DZone.

Arel & ActiveRecord: The Definitive Guide to Ruby's SQL Manager

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

If you're a Ruby developer working with SQL, you might want to check out Jiří Pospíšil's definitive guide to Arel, the SQL manager for Ruby. It's one of those libraries that you use constantly and rely on heavily, even though you might not know much about how it works, or even that it exists.  Pospíšil describes Arel as follows:

It’s all about providing frameworks with a way of building and representing SQL queries. It’s not the kind of library you would typically want to use directly (although you could as shown in a minute). Arel is meant to be the basic building block upon which frameworks build their own APIs that are more suitable for the end user.

One such framework, as most Ruby devs are certainly aware, is ActiveRecord, Rails' default ORM. Pospíšil uses ActiveRecord for most of his examples, and goes into great detail explaining the inner workings of Arel and, more importantly, Arel's relationship with ActiveRecord:

The interesting thing is the collaboration between Arel and ActiveRecord. Arel is technically independent from ActiveRecord but it needs to get the database details from somewhere and currently it uses ActiveRecord. More specifically, Arel requires ActiveRecord’s APIs. There’s even a fake ActiveRecord implementation, FakeRecord, that is used to run the Arel’s tests.

Pospíšil explores basic SELECT statements, various JOINs, the limitations of the technology, and more, pointing out the interactions between Arel and ActiveRecord for each. He evens walks you through working directly with the underlying Arel objects, even though that sort of cleverness isn't currently supported.

If you're interested in getting to know your Ruby in a bit more of an under-the-hood fashion, Pospíšil's guide to Arel is a great place to start.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}