Neil Chaudhuri
unread,Apr 21, 2012, 1:30:57 AM4/21/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to DataMapper
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.