📝 Common Table Expressions in Ruby on Rails
Published on .
A practical look at Common Table Expressions in Ruby on Rails. Covers writing CTEs as raw SQL in Rails 7.0 and below, the N+1 query problem that raw SQL results introduce (because ActiveRecord's preload cannot be used), a workaround that calls the preloader directly, and the cleaner approach added in Rails 7.1 — the ActiveRecord-native "with" method, which generates the CTE and restores full preload support.