Skip to content

Instantly share code, notes, and snippets.

@emmanuel
Created June 2, 2011 22:26
Show Gist options
  • Select an option

  • Save emmanuel/1005469 to your computer and use it in GitHub Desktop.

Select an option

Save emmanuel/1005469 to your computer and use it in GitHub Desktop.
DataMapper: an opportunity for optimization: don't need to retrieve existing join resources when replacing them
require "rubygems"
require "datamapper"
DataMapper::Logger.new($stdout, :default)
DataMapper.setup(:default, "sqlite::memory:")
class Source
include DataMapper::Resource
property :id, Serial
has n, :joins
has n, :targets, :through => :joins
end
class Join
include DataMapper::Resource
belongs_to :source, :key => true
belongs_to :target, :key => true
end
class Target
include DataMapper::Resource
property :id, Serial
has n, :joins
has n, :sources, :through => :joins
end
DataMapper.auto_migrate!
s = Source.create
10.times { Target.create }
s.targets = Target.all
s.save
s.targets = [Target.first]
s.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment