Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
153 lines (102 sloc) 4.43 KB

Arel Build Status Dependency Status


Arel Really Exasperates Logicians

Arel is a SQL AST manager for Ruby. It

  1. Simplifies the generation of complex SQL queries
  2. Adapts to various RDBMSes

It is intended to be a framework framework; that is, you can build your own ORM with it, focusing on innovative object and collection modeling as opposed to database compatibility and query generation.


For the moment, Arel uses Active Record's connection adapters to connect to the various engines, connection pooling, perform quoting, and do type conversion.

A Gentle Introduction

Generating a query with Arel is simple. For example, in order to produce


you construct a table relation and convert it to sql:

users =
query = users.project(Arel.sql('*'))

More Sophisticated Queries

Here is a whirlwind tour through the most common SQL operators. These will probably cover 80% of all interaction with the database.

First is the 'restriction' operator, where:

# => SELECT * FROM users WHERE = 'amy'

What would, in SQL, be part of the SELECT clause is called in Arel a projection:

# => SELECT FROM users

Joins resemble SQL strongly:

# => SELECT * FROM users INNER JOIN photos ON = photos.user_id

What are called LIMIT and OFFSET in SQL are called take and skip in Arel:

users.take(5) # => SELECT * FROM users LIMIT 5
users.skip(4) # => SELECT * FROM users OFFSET 4

GROUP BY is called group:


The best property of arel is its "composability", or closure under all operations. For example, to restrict AND project, just "chain" the method invocations:

users                                 \
  .where(users[:name].eq('amy'))      \
  .project(users[:id])                \
# => SELECT FROM users WHERE = 'amy'

All operators are chainable in this way, and they are chainable any number of times, in any order.


The OR operator works like this:


The AND operator behaves similarly.

The Crazy Features

The examples above are fairly simple and other libraries match or come close to matching the expressiveness of Arel (e.g., Sequel in Ruby).

Inline math operations

Suppose we have a table products with prices in different currencies. And we have a table currency_rates, of constantly changing currency rates. In Arel:

products =
# Attributes: [:id, :name, :price, :currency_id]

currency_rates =
# Attributes: [:from_id, :to_id, :date, :rate]

Now, to order products by price in user preferred currency simply call:

  where(currency_rates[:to_id].eq(user_preferred_currency), currency_rates[:date].eq(
  order(products[:price] * currency_rates[:rate])

Complex Joins

Where Arel really shines in its ability to handle complex joins and aggregations. As a first example, let's consider an "adjacency list", a tree represented in a table. Suppose we have a table comments, representing a threaded discussion:

comments =

And this table has the following attributes:

# [:id, :body, :parent_id]

The parent_id column is a foreign key from the comments table to itself. Now, joining a table to itself requires aliasing in SQL. In fact, you may alias in Arel as well:

replies = comments.alias
comments_with_replies = \
# => SELECT * FROM comments INNER JOIN comments AS comments_2 WHERE comments_2.parent_id =

This will return the first comment's reply's body.


Arel is released under the MIT License.

Something went wrong with that request. Please try again.