Skip to content

Instantly share code, notes, and snippets.

@jpr5
Created Jan 5, 2011
Embed
What would you like to do?
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