Search⌘ K
AI Features

Solution: The Model Has an Active Record

Explore how to improve application design by decoupling models from Data Access Objects such as Active Record. Understand object-oriented principles like low coupling, high cohesion, and aggregation to build clearer, maintainable domain models. Learn to encapsulate database interactions within models, ease unit testing, and simplify controller logic for better productivity in database-driven applications.

Controllers handle application input while views handle application output, both relatively simple and well-defined tasks. Frameworks are best at helping us put these together quickly. But it’s hard for a framework to provide a one-size-fits-all solution for models because models comprise the rest of the object-oriented design for our application.

This is where we actually need to identify what the objects are in our application and what data and behavior those objects have. It’s true what Robert L. Glass said:the majority of software development is intellectual and creative.

Grasping the model

Fortunately, there’s a lot of wisdom in the field of object-oriented design to guide us. Craig Larman’s book Applying UML and PatternsCraig Larman. Applying UML and Patterns: an Introduction to Object-Oriented Analysis and Design and Iterative Development. Prentice-Hall, Englewood Cliffs, NJ, Third, 2004. describes guidelines called the General Responsibility Assignment Software Patterns (GRASP). Some of these guidelines are especially relevant to separating models from their data access objects:

Information expert

The object responsible for an operation should have all the data needed to fulfill that operation. Since some operations in our application involve multiple tables (or no tables) and Active Record is good at working ...