Created
January 5, 2011 00:27
-
-
Save jpr5/765715 to your computer and use it in GitHub Desktop.
DM example of M:M through join table using two relationships.
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
class DocumentSet | |
include ::DataMapper::Resource | |
property :id, Serial | |
# First, define the immediate (intermediate) relationship. | |
has n, :document_maps, :child_key => [:ds_id] | |
# Then make another relationship that bounces through that relationship | |
# (:through), arriving at model Document. | |
has n, :documents, :through => :document_maps, :via => :document | |
end | |
# This is the intermediate model. | |
class DocumentMap | |
include ::DataMapper::Resource | |
property :id, Serial | |
property :ds_id, Integer, :required => true, :index => true | |
property :document_id, Integer, :required => true, :index => true | |
belongs_to :document_set, :child_key => [:ds_id] | |
belongs_to :document | |
end | |
# Model you want to end up at. | |
class Document | |
include ::DataMapper::Resource | |
property :id, Serial | |
# Not necessary, but useful for going backwards. | |
has n, :document_maps, :child_key => [:document_id] | |
end | |
# Then run something like: | |
DocumentSet.first.documents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment