Arel

DESCRIPTION

Arel Really Exasperates Logicians

Arel is a SQL AST manager for Ruby. It

Simplifies the generation of complex SQL queries

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.

Status

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

Complex Joins

Where Arel really shines is 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=Arel::Table.new(: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.
Joining a table to itself requires aliasing in SQL. This aliasing can be handled from Arel as below: