开发者

Rails/Mysql/SQLite - What's the best idea for building 2- or 3-level menu

开发者 https://www.devze.com 2023-04-06 21:48 出处:网络
I think about the best way, how to create a 2- 3-level menu. My first idea is everything to store to one database table, with the following structure:

I think about the best way, how to create a 2- 3-level menu. My first idea is everything to store to one database table, with the following structure:

- id
- item
- level

If I create the first-level menu item, so level will 0. If I will create the menu item of second level (eg. Contact - phone), so phone is the second menu item and the value of level will be the ID of Contact. But now - I don't know, how to print it from database - in the first loop I will print the first level of menu items (with level=0) and t开发者_运维问答hen I should to print the second menu items (with level=ID_of_first_menu_item) - exist any elegant way, how to simultaneously print first and second level items (I will build it in Rails)?

And the second way - every level of menu items stored to separate table - but I mean this is not good idea...

Thanks for your help!


take a look at https://github.com/collectiveidea/awesome_nested_set which is basically a tree. suits perfectly for nested navigation

0

精彩评论

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