Technical Blog: The Basics of Active Record

September 22st, 2014

Active Record: The "M" in MVP

Active Record is the "M" in MVC (Model-View-Controller) which is the layer of the system responsible for representing business data and logic. Before we get into Active Record, we need to dwelve into MVW, Model-View-Controller.

Model-View-Controller (MVP)

  1. Models
    • Models represent knowledge. A model could be a single object, or it could be some structure of objects. There should be a one-to-one correspondence between the model and its parts on the one hand, and the represented world as perceived by the owner of the model on the other hand.
  2. Views
    • A view is a visual representation of its model. It would ordinarily highlight certain attributes of the model and suppress others. It is thus acting as a presentation filter. A view is attached to its model (or model part) and gets the data necessary for the presentation from the model by asking questions. It may also update the model by sending appropriate messages. All these questions and messages have to be in the terminology of the model, the view will therefore have to know the semantics of the attributes of the model it represents.
  3. Controllers
    • A controller is the link between a user and the system. It provides the user with input by arranging for relevant views to present themselves in appropriate places on the screen. It provides means for user output by presenting the user with menus or other means of giving commands and data. The controller receives such user output, translates it into the appropriate messages and pass these messages on to one or more of the views.
    • The MVC Model

      A simple example of MVC is this:

      • Model = HTML
      • View = CSS
      • Controller = Browser

      Back to Active Record

      In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic is part of the object will educate users of that object on how to write to and read from the database.

      What things can Active Record do?

      1. Represents models and their data.
      2. Represent associations between these models.
      3. Represent inheritance hierarchies through related models.
      4. Validate models before they get persisted to the database.
      5. Perform database operations in an object-oriented fashion.