ActiveRecord::Refined
Adding clean and powerful query syntax on Active Record using refinements.
Author.
joins(:posts) { :posts[:author_id] == :authors[:id] }.
where { :authors[:age].in?(20..40) & (:posts[:published] == true) }
# SELECT "authors".* FROM "authors"
# INNER JOIN "posts" ON "posts"."author_id" = "authors"."id"
# WHERE "authors"."age" BETWEEN 20 AND 40 AND "posts"."published" = TRUE
Reference
- BlockSyntax — what a symbol answers to
inside a block:
as,asc,desc,collate,[], and through it the conditions of Predications and the operators of Arithmetics. - BlockContext — what a block can call:
the aggregates, the scalar and window functions,
CASE,sql,fn. - QueryMethods — what the relation
takes: the block forms of
whereand the rest, the joins,from_cte,distinct_on,lateral. - Writes —
update_allandupsert_allwith a block. - Dialect — one class per family of SQL
spellings, and
registerfor an adapter of your own.
Guides
One topic at a time, each with the SQL it builds and where the adapters differ:
- Conditions — comparisons,
LIKE,IN,NULL,true?, regular expressions, subqueries,ANYandALL, arrays. - Joins — the
ONas a block, table aliases, subqueries andLATERAL. - Aggregates and functions —
count,filter,string_agg, the scalar functions, the clock, durations,extractandcast. - Expressions — arithmetic, the bitwise operations,
CASE. - JSON —
dig,bury,except,key?,keys, the JSON aggregates, and where the adapters' JSON types part. - Window functions —
over,partition,order, frames. - Aliases, ordering and collation
- Grouping —
DISTINCT ON,GROUPING SETS,ROLLUP,CUBE. - Common table expressions —
with_recursiveandfrom_cte. - Writing —
update_allandupsert_all. - Time zones — what a block converts and what it leaves to the session.