オブジェクトの 1 つのリレーションに STI を使用しています (正しく、約束します!)。
class Walrus < ActiveRecord::Base
has_one :bubbles
end
class Bubbles < ActiveRecord::Base
belongs_to :walrus
before_save :set_origin
private
def set_origin
self.type = walrus.state ? "Bubbles::#{walrus.state}" : 'Bubbles'
end
end
class Bubbles::OfMind < Bubbles
def tango
end
end
新しいリレーションを作成すると、クラスが正しく設定されません。
harold = Walrus.new(state: 'OfMind')
harold.build_bubbles.save!
harold.bubbles
# => returns instance of Bubbles, not Bubbles::OfMind
harold.bubbles.tango
# NoMethodError
Bubbles オブジェクトは、魔法のように Bubbles::OfMind になることはできませんが、関係が正しいタイプになるまで、正しい機能は存在しません。