Talks
Speakers
Events
Topics
Sign in
Home
Talks
Speakers
Events
Topics
Leaderboard
Use
Analytics
Sign in
Suggest modification to this talk
Title
Description
The pain is familiar to long-time developers of enterprise Rails applications. Database queries through ActiveRecord are an essential part of our application. And making these queries performant, dynamic, and readable is hard. We can solve the above problems. With the right abstractions we can write code that composes queries that are performant, dynamic, and readable. Our approach uses a domain specific language built on top of ActiveRecord that you can adapt to your own application. We do not need a whole new library or framework. We just need query objects and builders.
Date
Summarized using AI?
If this talk's summary was generated by AI, please check this box. A "Summarized using AI" badge will be displayed in the summary tab to indicate that the summary was generated using AI.
Show "Summarized using AI" badge on summary page
Summary
Markdown supported
In the presentation titled "A Custom Design Pattern for Building Dynamic ActiveRecord Queries" given by Justin Daniel at RailsConf 2023, the focus is on overcoming the challenges developers face in writing and maintaining performant, dynamic, and readable database queries using ActiveRecord in Rails applications. This session proposes a unique design pattern that leverages abstraction to enhance the construction of queries without relying on external libraries. ### Key Points Discussed: - **Importance of Composable Queries**: Composable and maintainable queries are essential for meeting diverse data output demands across various platforms, including web pages and third-party integrations like Salesforce. Developers encounter difficulties ensuring these queries are efficient and user-friendly. - **Challenges with Legacy Code**: Relying on SQL concatenation results in cumbersome code that lacks readability and maintains complexity. Daniel illustrates this with a legacy code example totaling over 230 lines, which is hard to maintain and alters the comprehension of business logic. - **Design Pattern Overview**: Daniel introduces a custom design pattern using the Builder and Visitor patterns to create queries that follow object-oriented programming principles. This approach promotes modularity and readability by breaking queries down into smaller, manageable components, akin to Lego blocks. - **Comparison of Query Tools**: The speaker contrasts raw SQL, vanilla ActiveRecord, and external libraries like Aero tables, highlighting the pros and cons of each. While raw SQL is highly performant, it sacrifices readability and maintainability. Simple ActiveRecord queries are more comprehensible but struggle with complex conditions, and Aero tables, while effective, introduce dependency concerns and complex syntax. - **Builder Pattern Implementation**: The presenter delves into the mechanics of the query Builder and Visitor design pattern, showcasing how to encapsulate public interfaces within query classes, manage dependencies, and utilize a reducing method to construct ActiveRecord relations effectively. The pattern permits elegant handling of conditional logic through nodes that self-validate against user-driven filters. - **Nested Nodes and Complex Queries**: The capability to nest nodes allows for advanced filtering, which can significantly enhance the flexibility of queries, reviewing an example of creating conditions for legendary characters in a tabletop RPG. ### Conclusion and Takeaways: - The Builder pattern enhances object-oriented programming principles by allowing developers to tell a story with their code, fostering better maintainability and comprehension. - Daniel encourages developers to adopt this powerful abstraction as it enables creating dynamic queries while avoiding the pitfalls of legacy SQL concatenation. He emphasizes the balance between performance and readability as a key goal in query design. - In closing, Daniel invites the audience to explore the GitHub repository shared during the talk, which provides resources to implement the Builder pattern in their applications. Overall, the presentation aims to inspire Rails developers to explore new querying methodologies that improve developer efficiency and application performance.
Suggest modifications
Cancel