Everyday Active Record 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_saveandbefore_destroycallbacks to keep cache data consistent - use the
counter_cacheoption on associations - track dirty columns
- destroy dependent records
- apply other callback tips