当前位置: 动力学知识库 > 问答 > 编程问答 >

activerecord - groups and sub groups in rails model

问题描述:

I need to be able to have groups and unlimited number of subgroups under that group, and subgroups under that subgroup, etc...

I am trying to figure out the right way to do it.

Here is what I am thinking (with some flaws already)

I will create a group model that has id, group_name, and parent_id.

In another table I would store the bottom most id from the groups table, and that should be able to get me back to the main group.

My issue with this is, I don't know how to (in rails) associate them back up to the top level from the same table.

I guess I am trying to do a has_one on the same table I am working in (maybe not)

I have no idea how to make this work, and would appreciate any help / feedback on my idea.

网友答案:

I would suggest you to check nested set stuff: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

There are some implementations already, for instance: https://github.com/collectiveidea/awesome_nested_set

分享给朋友:
您可能感兴趣的文章:
随机阅读: