I need to store people, albums, photos, and the order that photos
appear in an album, so I decided to try adding a "position" attribute
to the association between Photo and Album. Consider the following
models:

class Person
  include DataMapper::Resource
...
end

class Album
  include DataMapper::Resource
...
  has n, :photos, "Photo", :through => :album_photos, :via => :photo
end

class Photo
  include DataMapper::Resource
...

  belongs_to :person, :required => true
  has n, :albums, "Album", :through => :album_photos, :via => :album
end

class AlbumPhoto
  include DataMapper::Resource

  property :position, Integer, :default => 1

  belongs_to :album, :key => true
  belongs_to :photo, :key => true

end

First, is such the right way to achieve this sort of thing? I didn't
see anything like this in the documentation.

To add a wrinkle, I have a model called Book which has a 1:M
relationship with Album. When I execute the following code

    album.album_photos.destroy
    album.album_photos << AlbumPhoto.new({:position =>3})
    book.update

I get "DataMapper::UpdateConflictError: Book#update cannot be called
on a dirty resource"

I am curious if my models are defined correctly. If so, how can I
delete all prior associations and update their positions "cleanly"?

Thanks.


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

Reply via email to