Ah source code looks crappy here, here's a gist: https://gist.github.com/903276

On Apr 5, 10:56 am, Piotr Solnica <piotr.soln...@gmail.com> wrote:
> Hey Nevin,
>
> We forgive you ;)
>
> Anyway, here's what you can do:
>
> 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 ])
>
> # fetch all the photos from the photo set that has "red" tag
> photo_set_red.photos
>
> # fetch all the photos from the photo set that has "green" and "blue"
> tags
> photo_set_green_and_blue.photos
>
> Please also remember that you can use dm-taggings plugins that does
> 90% of the above.
>
> Hope this helps!
>
> Cheers
>
> # solnic
>
> On Apr 5, 9:52 am, Nevin <nevin...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi,
>
> > Please forgive me that I'm pretty new to datamapper :-P
>
> > When I reading the associations document in the datamapper web, it
> > shows how to do a mapping between Photo <--> Tag via the Tagging
> > class.
>
> > What if I want to further extend the model so that a Set class is
> > created to include Photos? The idea is to able to add Tags into a Set
> > so I can retrieve all the Photos that have the same Tags.
>
> > Sets <--- tagging ---> Tags
> > Photos <--- tagging ---> Tags
>
> > So ultimately:
> > Set <--- Tags? Tagging? ---> Photos
>
> > Thanks :-)

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to datamapper@googlegroups.com.
To unsubscribe from this group, send email to 
datamapper+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to