← Blog

2 hours ago

Arel Basics

By Amanda Klusmeyer

I was tasked with figuring out why unexpected behavior was happening in our app. I started by following the bread crumb trail looking for the source and stumbled upon Arel. No, not the “under the sea” Ariel unfortunately. I was looking at a block of code like this:

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

I had significantly more question than when I started. Mostly those questions could be boiled down to: “what the heck is this?” and “how the heck does it work?”.

The tried and true method for any developer stuck on a problem, I googled my problem. Arel stands for “a relational algebra”. If you are like me, this did not clear up any of my questions but it at least let me know it wasn’t a random sequence of letters smooshed together.

What was super helpful, was finding a RubyConf video from 2014 and this more advanced blog post , all about Arel. I will summarize these for you in hopes that other developers out there will better understand what Arel is. This will be a beginners dive or a “dip your toe in the water” if you will. Warning, this is not all inclusive. Just the tip of the iceberg and ramblings from a junior developer 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, that 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”. To put it more succinctly now that you have all the terms, 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 (or sea if we are going with the water theme still)” 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. An example of what that means is the image below that I found from a google image search. It says: Select values, id and name, from the Users table

Let’s look at an example of SQL and Arel side by side to get a visual of what this all means.

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 how much more readable/writable Arel is in comparison.

In summary, Arel is SQL in a ruby trench coat. I wish I could draw because that would be an amazing image to insert into this blog post. Arel allows Ruby developers to write cleaner, more understandable code instead of having to learn the complex and harder to read syntax of SQL. All in all, a great alternative to SQL until you need to do something more complex.

Psst! We're always looking for people, like you, who are interested in learning and motivated to get better to join our remote team. Get in touch!