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.
About this Screencast
After watching these video tutorials, you’ll be able to quickly design models for your application and map them efficiently to your database. You’ll find yourself writing leaner, meaner code. Better yet, you’ll understand why it all works. If you have an existing application, you’ll likely learn new tricks you can immediately apply to your models. All your Rails applications will benefit from learning these techniques.
In addition to the videos, you can also download the source code for each episode so you can experiment on your own. All the source code is based on the latest version of Rails (2.2.2).
Contents and 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
- 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
findmethod with several options, including the new Rails 2
named_scopemethod. You’ll learn how to:
- perform finds on models using various
- write class methods to encapsulate complex find operations
- use the new Rails 2
named_scopemethod 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
- perform finds on models using various
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 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
before_destroycallbacks to keep cache data consistent
- use the
counter_cacheoption 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
findoptions 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
:selectto speed up queries by limiting which columns are selected
:includeto eliminate N+1 query problems
:joinsto 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.
Comments and Reviews
Tim Sullivan said:
The Everyday Active Record screencasts are just what I needed to help solidify everything together.
—Matt Polito Organizer at ChicagoRuby.org
This new series is fantastic!