Active Record models are the foundation of every good Rails application. Learn how to get the most out of the Active Record features that you use on a day-to-day basis from a popular Rails developer and screencaster. In these screencasts, Ryan Bates incrementally builds the models for a Rails application and refactors them over time to address new features. All the code is based on Rails 2.1 features.

  • Watch the preview highlighting the application Ryan builds in these screencasts.
    (1 min, QuickTime | Ogg)
Everyday Active Record
Code Discussions FAQ
  • Download and watch DRM-free episodes when and where it's convenient for you
  • You get both QuickTime and iPod/iPhone formats, and Theora Ogg format for select topics
  • All the source code is available

Buy Now

All the episodes in this screencast series have been released.

 

Contents

Throughout these screencast episodes, you’ll get inside the mind of an experienced Rails developer: his thought processes, his development techniques, the tools he uses, and the tricks of the trade. You can follow along as he adds new features to shape the application over time. You’ll also see how he troubleshoots problems along the way. You’ll not only learn more about Active Record, you’ll also see ways to streamline your development workflow.

  • Episode 1: Designing Models with Associations

    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

    We’ll pick up where we left off by fetching models using the find method with several options, including the new Rails 2.1 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.1 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

    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

    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

    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

Audience

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

About the Screencaster

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.