Skip to content

Instantly share code, notes, and snippets.

@tpitale
Created July 27, 2009 13:14
Show Gist options
  • Select an option

  • Save tpitale/156248 to your computer and use it in GitHub Desktop.

Select an option

Save tpitale/156248 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'dm-core'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class Sale
include DataMapper::Resource
property :id, Serial
belongs_to :user
has n, :sale_items
has n, :items, :through => :sale_items
end
class SaleItem
include DataMapper::Resource
property :id, Serial
belongs_to :sale
belongs_to :item
end
class Item
include DataMapper::Resource
property :id, Serial
has n, :sale_items
end
class User
include DataMapper::Resource
property :id, Serial
has n, :sales
has n, :sale_items, :through => :sales
# has n, :items, :through => :sale_items
end
Sale.auto_migrate!
# Sale is fine here
puts Sale.new.inspect
DataMapper.auto_migrate!
# Sale now has sale_item_id
puts Sale.new.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment