Interesting Things
- STI + Polymorphism needs base class
class Movie < ActiveRecord::Base has_one :player, :as => :content end class Player < ActiveRecord::Base belongs_to :content, :polymorphic => true end
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’