datamapper - many-to-many parent child relationship

Stamped: 2008-06-17 00:00:00 -0400 | Show comments

I started playing around with datamapper to see if I could model an many to many parent/child relationship (self-referential habtm?) for an upcoming project. The plan was to use sproutcore for the gui (once I figured it out!), and merb as a webservice that it interacts with.

Justin gave me a kickstart with datamapper, and I eventually came up with:

class Person
  include DataMapper::Resource

  property :id, Integer, :serial => true

  has n, :relationships
  has n, :parents, :through => :relationships, :class_name => 'Person'
  has n, :children, :through => :relationships, :class_name => 'Person'

  # parent = Person.create()
  # child = Person.create()
  # relationship = => parent)
  # child.relationships << relationship


class Relationship
  include DataMapper::Resource
  property :id, Integer, :serial => true
  belongs_to :person
  belongs_to :parent, :class_name => 'Person'
  belongs_to :child, :class_name => 'Person'

I'll eventually write some spec's for it.

tags: datamapper, habtm, merb
recent entries
Rails — A faster way for next and previous links on a post, article, or any model
The awkward things Siri says
Node.js — Getting oAuth Up and Running Using Express.js and Mongoose
Node.js — Getting oAuth Up and Running Using Express.js, Railway.js and Mongoose
Migrating from Rails 3.1 RC4 to RC5 using Heroku's Cedar Stack (also compass, unicorn, and sendgrid)
Random Freeze Fix for GTX 460 in 10.6 (osx86)
Wasted on Steam - an analytic tool for the Steam platform
Rails 3.1 — SQL logging to STDOUT during testing (with rspec, test::unit, or cucumber)
Rails 3.1 — Using ERB/HAML/etc within a Coffeescript JS file
Rails 3.1 — 'load_missing_contant': Expected ... to define ... (LoadError)
View the entire archive of articles