Created
August 28, 2008 21:53
-
-
Save halorgium/7835 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
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