Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created Jan 20, 2010
Embed
What would you like to do?
require 'rubygems'
require 'dm-core'
class Mother
include DataMapper::Resource
property :id, Serial
has 1, :baby
has 1, :child
end
class Baby
include DataMapper::Resource
property :id, Serial
property :name, String
property :mother_id, Integer, :required => false # avoiding some IntegrityErrors
property :type, Discriminator
end
class Child < Baby
end
#DataMapper::Logger.new(STDOUT)
DataMapper.setup(:default, "sqlite3::memory:")
DataMapper.auto_migrate!
e = Mother.create
e.baby = Baby.create( :name => "One" )
e.child = Child.create( :name => "Two" )
e.save
puts "Works, the associations is two separate objects like expected."
p e
p e.baby
p e.child
e = Mother.create
#e.baby = Baby.create( :name => "One" )
e.child = Child.create( :name => "Two" )
e.save
puts "Fails, both associations is the same object even though only one is set."
p e
p e.baby
p e.child # i'd expect this to be nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment