Skip to content

Instantly share code, notes, and snippets.

@somebee
Created September 17, 2009 18:54
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 somebee/188654 to your computer and use it in GitHub Desktop.
Save somebee/188654 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'dm-core'
DataMapper.setup(:default,
:adapter => 'mysql',
:host => 'localhost',
:username => 'root',
:database => 'dm_core_test',
:encoding => 'utf8'
)
DataObjects::Mysql.logger = DataObjects::Logger.new(STDOUT, :debug)
class Entity
include DataMapper::Resource
property :id, Serial
property :type, Discriminator
property :name, String
belongs_to :country, :nullable => true
end
class Company < Entity; end
class Person < Entity; end
class Country
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :entities
end
DataMapper.auto_migrate!
us = Country.create(:name => "USA")
hu = Country.create(:name => "Hungary")
c = Company.create(:name => 'Whole Foods', :country => us)
p = Person.create(:name => 'John')
p = Person.create(:name => 'Fredy', :country => hu)
Entity.all.each{|e| e.country}
# Thu, 17 Sep 2009 18:50:57 GMT ~ debug ~ (0.000160) SELECT `id`, `name` FROM `countries` WHERE `id` IN (1, NULL, 2) ORDER BY `id`
# /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:217:in `eager_load_targets': +source+ should be Company, but was Person (ArgumentError)
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/relationship.rb:293:in `block in eager_load'
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/relationship.rb:291:in `each'
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/relationship.rb:291:in `eager_load'
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:196:in `lazy_load'
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:96:in `get'
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:160:in `country'
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:452:in `each'
# from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:452:in `each'
# from relationship_2.rb:50:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment