Skip to content

Instantly share code, notes, and snippets.

@phatmann
Created January 16, 2009 19:53
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 phatmann/48081 to your computer and use it in GitHub Desktop.
Save phatmann/48081 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
gem 'dm-core', '~>0.9.9'
require 'dm-core'
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class Foo
include DataMapper::Resource
property :id, Serial
property :num, Integer
belongs_to :bar
end
class Bar
include DataMapper::Resource
property :id, Serial
property :num, Integer
has 1, :foo
end
DataMapper.auto_migrate!
foo = Foo.create(:num => 10)
bar = Bar.create(:num => 1)
foo.bar = bar
foo.save or raise "failed to save"
foo.bar.num = foo.num
raise "not dirtied" unless foo.bar.dirty?
foo.bar.save or raise "failed to save"
foo.bar.reload
puts "***** Failed to save using association" unless foo.bar.num == foo.num
bar = Bar.get(foo.bar_id)
bar.num = foo.num
bar.save or raise "failed to save"
bar.reload
puts "***** Failed to save using model" unless bar.num == foo.num
class Foo
def bar
@bar ||= Bar.get(self.bar_id)
end
end
foo.num = 11
foo.save
foo.bar.num = foo.num
raise "not dirtied" unless foo.bar.dirty?
foo.bar.save or raise "failed to save"
foo.bar.reload
puts "***** Failed to save using hacked association" unless foo.bar.num == foo.num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment