Mid-level Track Roadmap

Query objects: materialized

This course covers query objects and PostgreSQL materialized views in a Rails application. It starts with moving query logic out of fat models into plain Ruby classes that expose self.call and return a Data.define Result. It then explains materialized views as precomputed results stored on disk, how to create them with raw SQL or the scenic gem, and how to read them through read-only models. The last lessons cover refresh strategies, locking, staleness monitoring, testing and a production checklist.

What you'll learn

  • ✓How to extract query logic from fat models into query objects
  • ✓The self.call and Data.define Result contract for read-only queries
  • ✓When a materialized view fits and when a direct query is enough
  • ✓Creating materialized views with raw SQL migrations or the scenic gem
  • ✓Plain and concurrent refreshes, scheduling with Solid Queue and debouncing
  • ✓Testing query objects, view SQL and refresh jobs, plus monitoring in production
Free preview · Lesson 1 8 min read

What are query objects?

You open app/models/user.rb on a Monday morning. It's 847 lines long, with 34 scopes, 12 class methods that build queries and a self.dashboard_stats method of 60 lines of raw SQL stitched together with string interpolation. Three developers have added # TODO: refactor this, and nobody has.

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...