Friday, June 6, 2008

DataMapper: Parent / Child Relationship

I couldn't find an example at datamapper.org /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
ADAPTER
end

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 ]

end

Hope that helps someone else.

15 comments:

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

    ReplyDelete
  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."

    http://en.wikipedia.org/wiki/Hierarchical_model

    ReplyDelete
  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.

    Cheers

    ReplyDelete
  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.

    ReplyDelete
  5. Ah, I figured it out.

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

    pastie.

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

    http://github.com/datamapper/dm-more/tree/master/dm-is-tree

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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 https://relationadvisor.com/

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  14. Thank you so much as you have been willing to share information with us. We will forever admire all you have done here because you have made my work as easy as ABC. Learn More

    ReplyDelete
  15. I just want to let you know that I just check out your site and I find it very interesting and informative.. Here

    ReplyDelete