Skip to content

Instantly share code, notes, and snippets.

@markiz
Forked from FND/.gitignore
Created December 7, 2011 13:35
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 markiz/1442832 to your computer and use it in GitHub Desktop.
Save markiz/1442832 to your computer and use it in GitHub Desktop.
test case for apparent DataMapper bug when recursively saving associations
Gemfile.lock
source :rubygems
DM_VERSION = "1.2.0"
gem "dm-core", DM_VERSION
gem "dm-constraints", DM_VERSION
gem "dm-migrations", DM_VERSION
gem "dm-sqlite-adapter", DM_VERSION
#!/usr/bin/env ruby
# encoding: UTF-8
# test case for apparent DataMapper bug when recursively saving associations
require "rubygems"
require "dm-core"
#require "dm-constraints"
require "dm-migrations"
#DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, "sqlite::memory:")
DataMapper::Model.raise_on_save_failure = true
# models
class Person
include DataMapper::Resource
property :id, Serial
has 1, :pet, "Dog", :child_key => :owner_id
end
class Dog
include DataMapper::Resource
property :id, Serial
belongs_to :owner, "Person"
has n, :parasites, :child_key => :host_id
end
class Parasite
include DataMapper::Resource
property :id, Serial
belongs_to :host, "Dog"
end
DataMapper.finalize
DataMapper.auto_migrate!
# generate data
bug = Parasite.new
pet = Dog.new :parasites => [bug]
guy = Person.new :pet => pet
puts "[INFO] SAVING"
guy.save
puts [guy, pet, bug].map(&:saved?).join(" / ")
puts [guy, pet, bug].map(&:inspect)
puts bug.host_id # nil!
puts bug.dirty? # true => unsaved!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment