# The Power of Arel

DevFeed: [The Power of Arel](<https://devfeed.tech/articles/the-power-of-arel-21067.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/03/13/the-power-of-arel/>)

Published: 2016-03-13T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Object-relational mapping](<https://devfeed.tech/topics/orm.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [SQL](<https://devfeed.tech/topics/sql.md>), [Databases](<https://devfeed.tech/topics/databases.md>), [web applications](<https://devfeed.tech/topics/web-applications.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [orm](<https://devfeed.tech/tags/orm.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>), [sql](<https://devfeed.tech/tags/sql.md>), [web-applications](<https://devfeed.tech/tags/web-applications.md>)

## AI overview

This tutorial explains how Arel, the relational algebra library underlying ActiveRecord, helps Ruby on Rails developers construct complex database queries in pure Ruby. It compares Arel-based query composition with raw SQL, including filtering by dates and disambiguating columns when joining tables.

## Source excerpt

Many modern web applications have at least a few overlapping responsibilities. One of these responsibilities deals with maintaining state. A common choice for storing this state is by way of a relational database. Ruby on Rails applications assume the presence of a database by default and communicate with it via ActiveRecord. ActiveRecord is an Object Relational Mapping (ORM). Under the hood, ActiveRecord uses a relational algebra library called Arel to compose queries for execution. Arel is a very powerful library readily available for when ActiveRecord falls short. The Basics Keeping up with tradition, assume a standard Ruby on Rails application exists with at least two models, User and Post: create_table "posts" do |t| t.integer "user_id" t.string "category" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users" do |t| t.string "first_name" t.string "last_name" t.string "email" t.datetime "created_at", null: false t.datetime "updated_at", null: false end class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end Ask: Find all posts within a category belonging to a user. For such a simple query, ActiveRecord is capable of generating the appropriate SQL statement: Post.where(user_id: 123, category: 'news') .to_sql # => SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 123 AND "posts"."category" = 'news' However, if the query needed to be more complex, this simple where clause is not sufficient. For example, instead of all posts within a category for a user, only the posts which have a created_at in the current month are desired. A naive ActiveRecord only solution might look like: beginning_of_month = Date.today .beginning_of_month Post.where(user_id: 123, category: 'news') .where('created_at >= ?', beginning_of_month) .to_sql # => SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 123 AND "posts"."category" = 'news' AND (created_at >= '2016-03-01' ) This query will