Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created January 20, 2010 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slaskis/31594a7c27768a9dc879 to your computer and use it in GitHub Desktop.
Save slaskis/31594a7c27768a9dc879 to your computer and use it in GitHub Desktop.
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