Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'dm-core'
- DataMapper.setup(:default,
- :adapter => 'mysql',
- :host => 'localhost',
- :username => 'root',
- :password => '',
- :database => 'dm_core_test',
- :encoding => 'utf8'
- )
- DataObjects::Mysql.logger = DataObjects::Logger.new(STDOUT, 0)
- class Contact
- include DataMapper::Resource
- property :id, Serial
- property :type, Discriminator
- property :name, String
- has n, :notes
- end
- class Company < Contact
- end
- class Person < Contact
- end
- class Note
- include DataMapper::Resource
- property :id, Serial
- property :body, Text
- belongs_to :contact
- end
- Contact.auto_migrate!
- # See that it creates contact_id;
- # => CREATE TABLE `contacts` (`contact_id` INT(11), `id` INT(11) NOT NULL AUTO_INCREMENT, `type` VARCHAR(50), `name` VARCHAR(50), PRIMARY KEY(`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci
- Note.auto_migrate!
- company = Company.create(:name => "Enron")
- note = Note.create(:contact => company, :body => "This is a note")
- puts "reloading company and trying to access notes"
- company = Company.first
- # => SELECT `contact_id`, `id`, `type`, `name` FROM `contacts` WHERE `type` = 'Company' ORDER BY `id` LIMIT 1
- puts company.notes.inspect
- # => SELECT `contact_id`, `id`, `type`, `name` FROM `contacts` WHERE `contact_id` = 1 ORDER BY `id`
- puts "reloading note and trying to access company"
- puts Note.first.contact.inspect
- # Works as expected
Add Comment
Please, Sign In to add comment