A custom design pattern for building dynamic ActiveRecord queries
summary
A concrete pattern replaces a 230+ line SQL concatenation file with named query classes, builders, nodes that return ActiveRecord relations, use Ruby and Rails only, and run in the console and repo demo. The Builder uses reduce + visitor pattern to filter invalid nodes, merge clauses, and support nested nodes and subqueries. Three engineers build and iterate the repo, it composes conditional branching cleanly, stays performant, and is ready to adapt to real endpoints.
AI-written summary · about
chapters (27)
- 0:00Introduction
- 1:02Talk agenda
- 2:05Composable queries overview
- 3:34Dynamic query criteria
- 4:36Legacy SQL issues
- 5:41Builder pattern preview
- 6:23Query composition tools
- 9:35Custom Builder pattern
- 9:55Builder and Visitor deep dive
- 10:38Repository context
- 11:19Core components
- 13:28Nodes and conditions
- 14:39Builder methods
- 15:57Live query demo
- 17:30Builder takeaways
- 18:01Component interactions
- 19:32Reduce algorithm
- 21:16Why reduce works
- 22:19Builder with reduce and nodes
- 22:44Nested nodes metaphor
- 23:45Nested nodes example
- 26:23Builder best practices
- 28:32Modular node classes
- 29:19Builder abstraction
- 30:10Maintainable code
- 30:38Acknowledgments
- 31:23Question and answer session
related talks
33mExperimental Patterns in ActiveRecord
37mPatters to deal with big ActiveRecord models
35mAdvanced aRel: When ActiveRecord Just Isn't Enough
01h 47mWorkshop - Applications First, Frameworks Second: Better Systems through Design
29mActiveRecord, the Repository Pattern, and You
38mRefactoring Fat Models with Patterns
34mRefactoring Rails
31mIf it bleeds, it leads
