Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Yaneeka Huq

Posts By

Standup 7/16/2012: STI + Polymorphism needs base class

Interesting Things

  • STI + Polymorphism needs base class
class Movie < ActiveRecord::Base
  has_one :player, :as => :content

class Player < ActiveRecord::Base
  belongs_to :content, :polymorphic => true

and then, later we refactor Movie to use STI with Product...

class Product < ActiveRecord::Base ... end
class Movie < Product ... end

> movie = Movie.first
> movie.player
  Player Load (1.1ms)  SELECT "players".* FROM "players" WHERE "players"."content_id" = 144 AND "players"."content_type" = 'Product' LIMIT 1
 => nil

because players.content_type == 'Movie' but polymorphism uses the base class, 'Product'

Fix: Add a migration to change the content_type column

UPDATE players SET content_type='Product' WHERE content_type='Movie'