Skip to content

Instantly share code, notes, and snippets.

@tpitale
Created July 27, 2009 21:57
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 tpitale/156762 to your computer and use it in GitHub Desktop.
Save tpitale/156762 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'dm-core'
require 'dm-aggregates'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class Retailer
include DataMapper::Resource
property :id, Serial
has n, :sales
has n, :items
has n, :users
end
class Sale
include DataMapper::Resource
property :id, Serial
belongs_to :retailer
end
class Item
include DataMapper::Resource
property :id, Serial
belongs_to :retailer
end
class User
include DataMapper::Resource
property :id, Serial
belongs_to :retailer
end
DataMapper.auto_migrate!
r1 = Retailer.create
r2 = Retailer.create
100.times do |i|
Sale.create(:retailer => r1)
Item.create(:retailer => r1)
User.create(:retailer => r1)
end
100.times do |i|
Sale.create(:retailer => r2)
Item.create(:retailer => r2)
User.create(:retailer => r2)
end
# the first retailer will SEL all sales, items, users
# which isn't much here, but can grow quite large
# when all that's needed is count
Retailer.all.each do |r|
puts r.sales.count
puts r.items.count
puts r.users.count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment