Skip to content

Instantly share code, notes, and snippets.

@halorgium
Created August 28, 2008 21:53
Show Gist options
  • Save halorgium/7835 to your computer and use it in GitHub Desktop.
Save halorgium/7835 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'dm-core'
require 'pp'
DataMapper.setup(:default, "sqlite3::memory:")
class City
include DataMapper::Resource
property :id, Serial
property :name, String, :nullable => false
end
class Person
include DataMapper::Resource
property :id, Serial
property :name, String, :nullable => false
belongs_to :birth_place, :class_name => 'City'
belongs_to :work_place, :class_name => 'City'
end
DataMapper.auto_migrate!
pp joe = Person.create(:name => 'Joe')
pp betty = Person.create(:name => 'Betty')
pp sf = City.create(:name => "San Fran")
pp la = City.create(:name => "Los Angeles")
joe.birth_place = sf
joe.work_place = la
pp joe.save
pp joe
betty.birth_place = sf
betty.work_place = sf
pp betty.save
pp betty
# Both associations use the 'city_id' property without any change for overriding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment