Created
April 5, 2011 08:57
-
-
Save solnic/903276 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env ruby | |
| # | |
| # encoding: utf-8 | |
| require 'dm-core' | |
| require 'dm-migrations' | |
| DataMapper::Logger.new $stdout, :debug | |
| DataMapper.setup :default, "sqlite::memory:" | |
| class Photo | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :caption, String | |
| has n, :photo_tags | |
| has n, :tags, :through => :photo_tags | |
| end | |
| class PhotoTag | |
| include DataMapper::Resource | |
| belongs_to :photo, :key => true | |
| belongs_to :tag, :key => true | |
| end | |
| class PhotoSet | |
| include DataMapper::Resource | |
| property :id, Serial | |
| has n, :photo_set_tags | |
| has n, :tags, :through => :photo_tags | |
| has n, :photos, :through => :tags | |
| end | |
| class PhotoSetTag | |
| include DataMapper::Resource | |
| belongs_to :photo_set, :key => true | |
| belongs_to :tag, :key => true | |
| end | |
| class Tag | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String, :unique => true | |
| has n, :photo_tags | |
| has n, :photos, :through => :photo_tags | |
| has n, :photo_set_tags | |
| has n, :photos, :through => :photo_tags | |
| end | |
| DataMapper.finalize | |
| DataMapper.auto_migrate! | |
| red_tag = Tag.create(:name => 'red') | |
| green_tag = Tag.create(:name => 'green') | |
| blue_tag = Tag.create(:name => 'blue') | |
| Photo.create(:caption => "Tagged with red", :tags => [ red_tag ]) | |
| Photo.create(:caption => "Tagged with green", :tags => [ green_tag ]) | |
| Photo.create(:caption => "Tagged with green and blue", :tags => [ green_tag, blue_tag ]) | |
| photo_set_red = PhotoSet.create(:tags => [ red_tag ]) | |
| photo_set_green_and_blue = PhotoSet.create(:tags => [ green_tag, blue_tag ]) | |
| puts photo_set_red.photos.inspect | |
| puts photo_set_green_and_blue.photos.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment