Junior Track Roadmap

ActiveRecord querying: includes & joins

This course covers how ActiveRecord queries related data across tables. It starts with .merge, which reuses a scope from a joined model. It shows why joins with .count returns duplicate rows and how .distinct fixes the count. It covers where.associated and where.missing as named existence filters. It explains eager loading through a has_many :through association and a polymorphic association, including why eager_load raises an error there. It ends with strict_loading, which raises an error instead of running a lazy query, and a scenario that combines every mechanic.

What you'll learn

  • ✓Reusing a scope from a joined model with .merge
  • ✓Avoiding duplicate rows in aggregates over joins with .distinct
  • ✓Filtering by association existence with where.associated and where.missing
  • ✓Eager loading has_many :through and polymorphic associations
  • ✓Raising on lazy loading with strict_loading
Free preview · Lesson 1 3 min read

.merge: reusing a scope from the joined model

The lesson "JOINs in Rails: ActiveRecord associations", in SQL: JOIN fundamentals, is thorough. It covers joins (an INNER JOIN that filters and doesn't load), left_joins (a LEFT OUTER JOIN, including anti-joins), and the three loading strategies includes, eager_load and preload. It walks through…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...