since 1999


1 minutes estimated reading time.

The Clean-up Refactor Deleting ".arel_tables"

Alex Piechowski

Sometimes, when composing queries that utilize arel, the queries can start to include a multitude of columns across a couple of tables. This can lead to longer lines due to the repetitive use of .arel_tables[]


We can create something that is a little more elegant and makes .arel_table[] feel as if it were a part of the ActiveRecord interface a little more directly. This can be done by creating a class method, #[], on our ApplicationRecord class like so:

# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def self.[](column)

This allows us to re-write our above code like so:


In the projects I’ve worked on, this tiny method adds a lot of value for codebases using complex queries.

What do you think?