Skip to content

Instantly share code, notes, and snippets.

@FND
Created December 7, 2011 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FND/1442692 to your computer and use it in GitHub Desktop.
Save FND/1442692 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"
# models
module Plan
class Parameter
include DataMapper::Resource
property :id, Serial
belongs_to :parameter, "Parameter::Base"
end
end
module Parameter
class Base
include DataMapper::Resource
property :id, Serial
property :type, Discriminator
has n, :plan_parameters, "Plan::Parameter",
:child_key => "parameter_id",
:constraint => :protect
end
class Subgrouped < Base
has n, :subgroups, "Parameter::Subgroup",
:child_key => "subgrouped_parameter_id",
:constraint => :destroy
end
class Subgroup
include DataMapper::Resource
property :id, Serial
belongs_to :subgrouped_parameter, "Parameter::Subgrouped"
end
end
# setup
#DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, "sqlite::memory:")
DataMapper::Model.raise_on_save_failure = true
DataMapper.finalize
DataMapper.auto_migrate!
# generate data
s = Parameter::Subgroup.new
p = Parameter::Subgrouped.new :subgroups => [s]
pp = Plan::Parameter.new :parameter => p
puts "[INFO] SAVING"
pp.save
puts [pp, p, s].map(&:saved?).join(" / ")
puts [pp, p, s].map(&:inspect)
puts s.subgrouped_parameter_id # nil!
puts s.dirty? # true => unsaved!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment