Active Record Scopes and Arel
summary
Argues that using Arel instead of SQL fragments makes ActiveRecord scopes truly composable and database-portable, showing concrete code: Arel table columns with matches produce ILIKE on Postgres and LIKE elsewhere, in accepts arrays or ranges (generating IN or BETWEEN), and combining predicates with or or hash-style where avoids ambiguous column errors when merging scopes. Recommends class methods over lambda scopes for time-dependent or complex queries and mentions Squeel as a nicer predicate DSL.
AI-written summary · about
chapters (10)
- 0:00History of Rails Querying
- 2:11Introduction of named scopes
- 4:00Rails 3 querying changes
- 5:44Database compatibility issues
- 8:21Introduction to Arel
- 10:41Using Arel for complex queries
- 13:20Advanced Arel and hashes
- 15:55Avoiding SQL fragments
- 19:28Scope best practices
- 22:52Arel predicates and Squeel
related talks
25mActiveRecord can't do it? Arel can!
21mActiveRecord can't do it? Arel can!
35mAdvanced aRel: When ActiveRecord Just Isn't Enough
32mArel: The Ruby Relational Algebra Library
05mHow to use Arel? Wrong answers only!
17mWrangle Your SQL With Arel
38mSequel: When ActiveRecord is not enough
30mBuilding an ORM with AReL: Walking up the (AS)Tree.
