Friday, June 6, 2008

DataMapper: Parent / Child Relationship

I couldn't find an example at /docs for a parent child relationship.

After a little searching, I found the answer in the integration tests.  Here is the example:

class Node
include DataMapper::Resource

def self.default_repository_name

property :id, Integer, :serial => true
property :name, String

has n, :children, :class_name => 'Node', :child_key => [ :parent_id ]
belongs_to :parent, :class_name => 'Node', :child_key => [ :parent_id ]


Hope that helps someone else.


  1. It looks more like a tree relationship then a parent/child relationship.

  2. "In a hierarchical data model, data is organized into a tree-like structure. The structure allows repeating information using parent/child relationships: each parent can have many children but each child only has one parent."

  3. This is very helpful, though I'm not too familiar with datamapper. I'm trying to see if it'll fit what I need.

    Could this be expanded to the parent becomes,
    has n, :children, :class_name => 'Node', :child_key => [:child_id]
    has n, :parents, :class_name => 'Node', :child_key => [:parent_id ]

    Something like that would work with activerecord, but, like I said I'm not too familiar.


  4. scott,

    Actually I believe the syntax you suggested is what I first tried, and did not work, and caused the searching.

    DataMapper is still somewhat young, so check the integration tests referenced in the post for the most recent developments.

  5. Ah, I figured it out.

    I didn't write a spec for it, nor would it stop any nonsensical inbred relationships, but:


  6. Why not just use the dm-is-tree plugin?

  7. This comment has been removed by the author.

  8. We need lie detector tests for all government law enforcement officers at the city level, county level, state-level and federal level lie detector test

  9. By being fully and completely who we are, we then attract relationships that reflect back to us the fullness of our creative being website.

  10. they could have a perfectly happy marriage but want to add some spice or fun. After all, something doesn't need to be broken for it to get better

  11. It’s very excellent information and more real facts to provided that post.Thank you for sharing this information. 2018 hostgator coupons

  12. Tolerance simply means being able accommodate, being able to condone no matter what. Are you so disciplined that you don't take shit from anybody?Fort Worth escorts

  13. We Provide Our Customers With Latest and up-to-date Dumps Questions & Answers with 100% Exam Passing Guarantee. We Promise Exceptional Success in First Attempt. PSE-Endpoint exam dumps