date_trunc and Group by Month in ActiveRecord
Group by month in ActiveRecord with PostgreSQL date_trunc. Tested time zone handling, key types, missing months, and what groupdate adds.
Using PostgreSQL-specific features like DISTINCT ON, JSONB, window functions, and CTEs safely through ActiveRecord.
Group by month in ActiveRecord with PostgreSQL date_trunc. Tested time zone handling, key types, missing months, and what groupdate adds.
Write LIKE and ILIKE in ActiveRecord, escape input with sanitize_sql_like, and see which patterns use an index. Tested on Rails 8.1 and PostgreSQL 16.
Sort NULLs last in Rails: Arel nulls_last, the raw string trap on SQLite, an IS NULL fallback and the PostgreSQL index. Tested on Rails 8.1.1.
Use ROW_NUMBER, RANK and LAG in ActiveRecord for latest-per-group and top N queries. Tested SQL and Ruby on Rails 8.1.1 and PostgreSQL 16.
Use with_recursive in Rails 7.2+ to walk trees in one query. Tested on ActiveRecord 8.1.1 and PostgreSQL 16, with depth, path and cycle guards.
Convert SQL SELECT DISTINCT to ActiveRecord: distinct, pluck, DISTINCT ON and the PostgreSQL ORDER BY error, with tested Rails 8.1 examples.
where returns a relation, find_by returns one record or nil. Here's when each is correct, what breaks if you pick wrong, and Rails 7/8 production patterns.
Rails 8.2 for production apps: caching, query performance, concurrency changes, and what actually breaks at scale. No hype, just impact. Updated 2026.