Introduction
What is Arel?
Arel is a SQL generator library for Ruby. It allows us to write complex SQL queries in a semantic, reusable fashion by using an SQL AST (Abstract Syntax Tree-like) approach.
It represents the SQL syntax as nodes similar to how we use an expression tree to represent mathematical expressions.
Arel uses different predications (analogous to operators for the expression tree) such as count
, eq
, not_eq
, gt
, etc. to construct the SQL
What is the SQL FILTER clause?
The filter clause in SQL extends aggregate functions (sum, avg, count etc.) by an additional where clause.
Syntax:
Rails 7 added support for the FILTER
binary prediction to support and generate this filter clause in Arel
Before
After
Note: The filter clause is currently supported only for PostgreSQL(9.4+) and SQLite(3.30+) databases