Posted almost 3 years ago. Visible to the public.

Migrations

Reading

Exercise

In MovieDB, add two fields:

Actor#total_movies
This field should cache the total number of movies an actor stars in
Actor#first_movie_id
This field should cache the first movie to which the actor was ever assigned in MovieDB

Add callbacks to your ActiveRecord models so the field is always up to date. For the purpose of this exercise, please don't use the :counter_cache option and do everything manually instead.

Whenever you add a new column you need to take care of existing records. Add a migration that sets the total_movies and first_movie_id column for existing records. Write this migration in multiple styles:

  • Embedding ActiveRecord models into your migration
  • SQL statements (update "UPDATE actors SET ...")
  • Helper methods from the database adapter

Owner of this card:

Avatar
Henning Koch
Last edit:
over 1 year ago
by Pascal Schmid
Keywords:
rails, activerecord
Posted by Henning Koch to makandra Curriculum
This website uses cookies to improve usability and analyze traffic.
Accept or learn more