Skip to content

Instantly share code, notes, and snippets.

@snusnu
Forked from michael/has_n_through_resource.rb
Created January 14, 2010 00:56
Show Gist options
  • Save snusnu/276747 to your computer and use it in GitHub Desktop.
Save snusnu/276747 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 Quote
include DataMapper::Resource
property :id, Serial
has 0..n, :billings, :through => Resource
end
class Billing
include DataMapper::Resource
property :id, Serial
has 0..n, :quotes, :through => Resource
end
puts DataMapper::Model.descendants.inspect
DataMapper.auto_migrate!
puts DataMapper::Model.descendants.inspect
__END__
[ree-1.8.7-2009.10] mungo:extlib snusnu$ ruby foo.rb
#<DataMapper::Model::DescendantSet:0x10165a778 @ancestors=nil, @descendants=[Quote, Billing]>
~ (0.000038) SELECT sqlite_version(*)
~ (0.002727) DROP TABLE IF EXISTS "quotes"
~ (0.002216) DROP TABLE IF EXISTS "billings"
~ (0.002530) DROP TABLE IF EXISTS "billing_quotes"
~ (0.000024) PRAGMA table_info("quotes")
~ (0.002243) CREATE TABLE "quotes" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
~ (0.000047) PRAGMA table_info("billings")
~ (0.004413) CREATE TABLE "billings" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
~ (0.000022) PRAGMA table_info("billing_quotes")
~ (0.002807) CREATE TABLE "billing_quotes" ("quote_id" INTEGER NOT NULL, "billing_id" INTEGER NOT NULL, PRIMARY KEY("quote_id", "billing_id"))
#<DataMapper::Model::DescendantSet:0x10165a778 @ancestors=nil, @descendants=[Quote, Billing, BillingQuote]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment