Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

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

Yaneeka Huq
Monday, July 16, 2012

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’

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Add New Comment Cancel reply

Your email address will not be published.

Yaneeka Huq

Yaneeka Huq
San Francisco

Recent Posts

    Subscribe to Yaneeka's Feed

    Author Topics

    agile (1)
    • About
    • Case Studies
    • Team
    • Community
    • Careers
    • Contact
    • Labs
    • Events

    Contact Us

    contact@pivotallabs.com
    +1 415-77-PIVOT
    TwitterLinkedInFacebook

    Pivotal Tracker

    Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
    Visit pivotaltracker.com >