All Things Arel Basics.

A tip of the iceberg introduction and ramblings from a developer trying to make sense of abstract concepts like Arel

When tasked with figuring out why unexpected behavior was happening in our app, I followed the bread crumb trail to this block of code:

def trial_days
 Arel::Nodes::Multiplication.new(
    Arel::Nodes::NamedFunction.new('COALESCE',[arel_table[:trial_days], TRIAL_DAYS]),
    Arel::Nodes::SqlLiteral.new("INTERVAL '1 day'")
  )
end

Who even are you, Arel, if that is your real name?! A quick Google search told me Arel stands for “a relational algebra.” Cool, that clears nothing up. But I at least knew it wasn’t a random sequence of letters smooshed together.

What did help: a RubyConf video from 2014 and this in-depth blog post. I will summarize a beginners Arel dive that will hopefully help all my fellow junior developers trying to make sense of abstract concepts.

Let’s start with the definitions and need to know terms.

Arel is a ruby gem that builds SQL queries in an object oriented way. SQL is an acronym for Structured Query Language, and it is used to communicate with a database.

Unlike SQL, Arel does not interact with your database. Arel queries are created in Ruby, and are used to talk to ActiveRecord. Active Record, another ruby gem, is the interface that Rails gives you between the database and your application. For example: it allows you to use “User.all” vs “SELECT * FROM users”. In other words, Arel lets Ruby developers create queries in ruby that are then used by the Ruby application to interact with the database

Active Record uses Arel “under the hood” so that we as Ruby developers can perform complex queries without having to learn SQL. User.all is easier to remember, read and write than SELECT * FROM users. It also allows us to stay true to the reason Ruby was created: to write code that is easily readable and writable.

One important more advanced thing to understand about Arel is that is uses AST. AST stands for Abstract Syntax Tree, meaning a tree representation of the source code. See below for a quick visualization:

Now let’s look at an example of SQL and Arel side by side:

book = Books.arel_table
query = book[:checked_out].gteq( 5.days.ago ).and(book[:genre].eq("fiction"))
query.to_sql
#  "books"."checked_out" >= '2021-12-13 09:43:51.137290'
#    AND ("books"."genre" = 'fiction')

The first line creates the AST for the Books model. The next line creates the Arel query for Active Record to use and communicate with the database. The last line is converting the Arel query into SQL so we can compare and see how both queries are doing the same thing, but note how much more readable/writable Arel is in comparison.

In summary, Arel is SQL in a ruby trench coat, allowing Ruby developers to write cleaner, more understandable code instead of having to learn the complex and harder to read syntax of SQL.

If you’re looking for a team to help you discover the right thing to build and help you build it, get in touch.

Published on November 12, 2021