Skip to content

Instantly share code, notes, and snippets.

@jpr5
Created January 5, 2011 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpr5/765715 to your computer and use it in GitHub Desktop.
Save jpr5/765715 to your computer and use it in GitHub Desktop.
DM example of M:M through join table using two relationships.
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