Skip to content

Instantly share code, notes, and snippets.

@MarkMT
Created October 28, 2009 21:05
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 MarkMT/220838 to your computer and use it in GitHub Desktop.
Save MarkMT/220838 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -Ku
require 'rubygems'
require 'dm-core'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'mysql://localhost/testdb')
class User
include DataMapper::Resource
property :id, Serial
property :name, String
has 1, :address
end
class Address
include DataMapper::Resource
property :id, Serial
property :country, String
belongs_to :user
end
DataMapper.auto_migrate!
# Create a new user
u = User.new(:name => 'Bob')
p u.save # => true
# Read the record back from the db
u = User.first
# works ok the first time
p u.address # => nil
# runtime error the second time
p u.address
~ (0.000018) SET sql_auto_is_null = 0
~ (0.000011) SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'
~ (0.012688) DROP TABLE IF EXISTS `users`
~ (0.000747) DROP TABLE IF EXISTS `addresses`
~ (0.000164) SHOW TABLES LIKE 'users'
~ (0.000018) SHOW VARIABLES LIKE 'character_set_connection'
~ (0.000019) SHOW VARIABLES LIKE 'collation_connection'
~ (0.001213) CREATE TABLE `users` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(50), PRIMARY KEY(`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci
~ (0.000013) SHOW TABLES LIKE 'addresses'
~ (0.001118) CREATE TABLE `addresses` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `country` VARCHAR(50), `user_id` INT(10) UNSIGNED NOT NULL, PRIMARY KEY(`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci
~ (0.002135) CREATE INDEX `index_addresses_user` ON `addresses` (`user_id`)
~ (0.000124) INSERT INTO `users` (`name`) VALUES ('Bob')
true
~ (0.000019) SELECT `id`, `name` FROM `users` ORDER BY `id` LIMIT 1
~ (0.000022) SELECT `id`, `country`, `user_id` FROM `addresses` WHERE `user_id` = 1 ORDER BY `id` LIMIT 1
nil
/usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/associations/one_to_many.rb:305:in `inverse_set': undefined method `readonly?' for nil:NilClass (NoMethodError)
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/associations/one_to_many.rb:293:in `resource_added'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/collection.rb:1202:in `resources_added'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/collection.rb:1202:in `map'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/collection.rb:1202:in `resources_added'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/collection.rb:649:in `set'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/associations/one_to_many.rb:284:in `new_collection'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/collection.rb:210:in `first'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/associations/one_to_one.rb:20:in `get'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.2/lib/dm-core/associations/relationship.rb:491:in `address'
from dmtest.rb:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment