Created
January 16, 2009 19:53
-
-
Save phatmann/48081 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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