Wrangle Your SQL With Arel by Eric Hayes
This is a fascinating talk that walks through the underlying APIs used to build SQL queries in ActiveRecord. Eric does a great job showing how ORMs use Abstract Syntax Trees to create SQL queries from hashes and method calls.
The end result shows how they built an incredibly complex cohort query with efficiency and maintainablity using Arel.