#1041 ✓resolved
Ashley Moran

STI subclass can't construct inherited child resource

Reported by Ashley Moran | September 15th, 2009 @ 11:03 AM

This appears to be a 0.10RC2 regression. As usual, it's much easier to explain with an RSpec example. Note that it only affects has 1 relationships and not has n.

require 'rubygems'

DM_GEMS_VERSION   = "0.10.0"
DO_GEMS_VERSION   = "0.10.0"
 
gem "data_objects",   DO_GEMS_VERSION
gem "do_sqlite3",     DO_GEMS_VERSION
gem "dm-core",        DM_GEMS_VERSION   
gem "dm-types",       DM_GEMS_VERSION        
gem "dm-validations", DM_GEMS_VERSION  

require "data_objects"
require "dm-core"
require "dm-types"
require "dm-validations"

require 'spec'
 
DataMapper.setup(:default, "sqlite3::memory:")

class Hat
  include DataMapper::Resource
  
  property :id, Serial
  property :type, String
  
  belongs_to :person
end

class Person
  include DataMapper::Resource

  property :id, Serial
  property :type, Discriminator
  
  has 1, :hat
  
  def make_hat    
    self.hat = Hat.new(:type => "felt")
  end
end

class Employee < Person
  
end
 
Spec::Runner.configure do |config|
  config.before(:each) do
    Hat.auto_migrate!
    Person.auto_migrate!
  end
end

describe "STI sublass can't construct inherited child" do
  it "FAILS if you try to make the hat from the STI subclass" do
    @employee = Employee.new
    @employee.make_hat
    @employee.hat.type.should == "felt"
  end
  
  it "PASSES if you try to make the hat from the STI base class" do
    @person = Person.new
    @person.make_hat
    @person.hat.type.should == "felt"
  end
end

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Pages