small medium large xlarge

Everyday Active Record


Cover image for Everyday Active Record

Everyday Active Record


Active Record models are the foundation of your Rails application. With a well-designed layer of models, your application is solid. You can make business logic changes easier, keep the integrity of your data in check, and better optimize the interaction with your database. Learn how to get the most out of the Active Record from Ryan Bates, one of the most experienced Rails developer (and screencasters) in the community.

Customer Reviews

The Everyday Active Record screencasts are just what I needed to help solidify everything together.

- Tim Sullivan

This new series is fantastic!

- Matt Polito

Organizer at

See All Reviews


Contents & Extracts

Throughout this series, we’ll build a movie theatre application, focusing specifically on the model layer.

  • Episode 1: Designing Models with Associations (27 minutes)

    We’ll start by creating the Movie, Review, Showing, and Theatre models, then knit them together with associations to bring the cinemas application to life. You’ll learn how to:

    • create models with their respective database tables
    • use one-to-many and many-to-many associations
    • apply guidelines for when to choose a particular association
    • create a join model with a :through association
    • navigate between associated models
  • Episode 2: Finding and Scoping Models (32 minutes)

    We’ll pick up where we left off by fetching models using the find method with several options, including the new Rails 2 named_scope method. You’ll learn how to:

    • perform finds on models using various find options
    • write class methods to encapsulate complex find operations
    • use the new Rails 2 named_scope method to elegantly constrain queries and make controllers easier to read
    • perform calculations using the built-in calculation methods
    • use more advanced find options such as :include
  • Episode 3: Validating Models (29 minutes)

    Validations are the key to preserving data integrity in Rails. We’ll use both built-in and custom validations to make sure movie reviews are submitted with valid data. You’ll learn how to:

    • use built-in validation methods
    • handle form errors conveniently
    • add custom validations using the validate method
    • validate across multiple database columns
    • make validations conditional based on authentication
    • handle validations across multiple models that are associated
  • Episode 4: Using Callbacks (30 minutes)

    Callbacks are a way for your application to hook into the lifecycle of Active Record models. In this episode, we’ll use callbacks to solve two database performance problems. We’ll cache a movie’s review count and its average number of stars. You’ll learn how to:

    • use Active Record callback methods
    • make callbacks conditional
    • apply before_save and before_destroy
      callbacks to keep cache data consistent
    • use the counter_cache option on associations
    • track dirty columns
    • destroy dependent records
    • apply other callback tips
  • Episode 5: Optimizing Queries (26 minutes)

    Rails makes it easy to fetch records from the database without even writing SQL, but it’s equally easy to wind up with slow database queries.
    In this episode, we’ll use advanced find options to improve the performance of database queries in the cinemas application. We’ll also
    measure each change along the way to avoid premature optimization. As a result, you’ll be able to benchmark and improve the performance of your database queries.
    You’ll learn how to:

    • create a staging environment for performance testing
    • dynamically populate the staging database to simulate the production environment
    • use :select to speed up queries by limiting which columns are selected
    • use :include to eliminate N+1 query problems
    • use :joins to join associations with faster queries and a smaller memory footprint
    • add database indexes and understand how they work
    • benchmark the performance benefits of the various tuning options


These screencasts are designed for beginner and intermediate Rails developers, but even the more experienced developers will get something out of the later episodes.


Ryan Bates has been involved in web development since 1998. In 2005 he started working professionally with Ruby on Rails and is now best known for his work on Railscasts, the free Ruby on Rails screencast series.