Skip to content

Instantly share code, notes, and snippets.

@knowtheory
Created February 15, 2011 16:47
Show Gist options
  • Select an option

  • Save knowtheory/827784 to your computer and use it in GitHub Desktop.

Select an option

Save knowtheory/827784 to your computer and use it in GitHub Desktop.
require 'dm-core'
require 'dm-sweatshop'
require 'dm-migrations'
class Foo
include DataMapper::Resource
property :id, Serial
property :name, String
property :sku, String
end
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, "mysql://localhost/test")
DataMapper.auto_migrate!
Foo.fix{{ :name => /\w+( \w+){0,2}/.gen, :sku => /\w+( \w+){0,2}/.gen}}
10.of{Foo.gen}
Foo.all.each{ |f| puts f.sku }
class Bar
include DataMapper::Resource
property :id, Serial
property :name, String
property :sku, String
belongs_to :foo
end
Foo.has(1.0/0, :bars)
Bar.auto_migrate!
Bar.fix{{ :name => /\w+( \w+){0,2}/.gen, :sku => /\w+( \w+){0,2}/.gen, :foo => Foo.pick}}
10.of{ Bar.gen }
Foo.all.map{ |f| f.bars.first }.size == Bar.all.map(&:foo_id).uniq.size
Foo.all.map{ |f| f.bars.first } # => SELECT `id`, `name`, `sku`, `foo_id` FROM `bars` WHERE `foo_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ORDER BY `id`
__END__
ruby-1.9.2-p0 :053 > Foo.all.map{ |f| b = f.bars.first; b.name if b }
~ (0.000202) SELECT `id`, `name`, `sku` FROM `foos` ORDER BY `id`
~ (0.007954) SELECT `id`, `name`, `sku`, `foo_id` FROM `bars` WHERE `foo_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ORDER BY `id`
=> [nil, "semipreservation oppositive", "tossment beguileful morologically", "unsolicitously oxyrhine agrarian", "hummeler fibromyotomy fiendly", nil, nil, "proustite delible", nil, "complimentingly kochliarion"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment