Created
June 2, 2011 22:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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