开发者

How to model family relationships in rails 3?

I'm working on a contact database in rails 3..

One thing thats really frustrating is how ugly the family relationship code is..

Is there a clean way of doing this in rails?

Basically all contacts are of the contact class (go figure!)

And contacts have many family_relationships (another model) and many relatives through family_relationships.. The family relationship model also has one family relationship type (another model)

So far i've implemented this using the methods here http://railscasts.com/episodes/163-self-referential-association (using inverse relationships etc..)

But this just doesnt fee开发者_如何学运维l very clean.. and if i want to get all the contacts relatives, relationships etc.. i have to drop to raw SQL or join the arrays..

Is there a better (or definitive) way that this kinda thing is done in rails?


The Ancestry gem seems like it solves exactly this kind of problem:

Ancestry is a gem/plugin that allows the records of a Ruby on Rails ActiveRecord model to be organised as a tree structure (or hierarchy). It uses a single, intuitively formatted database column, using a variation on the materialised path pattern. It exposes all the standard tree structure relations (ancestors, parent, root, children, siblings, descendants) and all of them can be fetched in a single sql query. Additional features are STI support, scopes, depth caching, depth constraints, easy migration from older plugins/gems, integrity checking, integrity restoration, arrangement of (sub)tree into hashes and different strategies for dealing with orphaned records.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜