Data Layer Abstractions
This chapter opens Part 2 of the book, in which we will talk about the particular patterns in extracting abstraction layers. As we discussed in Part 1, growing Rails applications feel too cramped within model-controller-view boundaries; introducing new abstractions allows your code to breathe freely. We start with the Rails model layer and its core component—Active Record.
In Chapter 2, Active Models and Records, we learned that Active Record models are by design responsible for representing domain objects and communicating with the persistence engine. In this chapter, we will discuss the techniques for reducing the responsibility of Active Record by introducing new abstraction layers. First, we will discuss how to keep the query-building functionality organized via query objects. Then, we try to leverage Data Mapper ideas and use repositories for Active Record models.
We will cover the following topics:
- Using query objects to extract (complex...