Skip to content

Instantly share code, notes, and snippets.

@xxx
Created October 4, 2009 02: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 xxx/201082 to your computer and use it in GitHub Desktop.
Save xxx/201082 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'dm-core'
class Foo
include DataMapper::Resource
property :id, Serial
property :name, String
has 1, :bar
has n, :bazs
end
class Bar
include DataMapper::Resource
property :id, Serial
property :name, String, :nullable => false
belongs_to :foo, :nullable => true
end
class Baz
include DataMapper::Resource
property :id, Serial
property :name, String
belongs_to :foo
end
DataMapper.setup(:default, 'sqlite3::memory:')
DataMapper.auto_migrate!
baz1 = Baz.new(:name => 'baz1')
baz2 = Baz.new(:name => 'baz2')
foo = Foo.new(:name => 'foo')
foo.bazs.new(baz1.attributes)
foo.bazs.new(baz2.attributes)
bar = Bar.create(:name => 'bar', :foo => foo)
p bar.dirty? # true
bar.name = 'hey'
p bar.dirty? # true
p bar.save # true
#bar.reload
p bar.dirty? # true, whether you reload or not
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment