Skip to content

Instantly share code, notes, and snippets.

@MarkMT
Created October 30, 2009 12:09
Show Gist options
  • Save MarkMT/222295 to your computer and use it in GitHub Desktop.
Save MarkMT/222295 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
# Assign a new address to the user
u.address ||= Address.new # => runtime error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment