https://github.com/collectiveidea/awesome_nested_set
修改Gemfile
gem 'awesome_nested_set'
1, 生成对应的文件
rails g scaffold category name:string parent_id:integer lft:integer rgt:integer depth:integer
2, 修改model
class Category < ActiveRecord::Base
acts_as_nested_set
attr_accessible :name, :parent_id
end
3, 修改views
<%= f.select :parent_id, nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" } %>
<%= select_tag 'parent_id', options_for_select(nested_set_options(Category) {|i| "#{'-' * i.level} #{i.name}" } ) %>
4, 如何取得指定category的所有的children和nest children
category.descendants
# Returns a set of itself and all of its nested children
def self_and_descendants
nested_set_scope.where([
"#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_left_column_name} < ?", left, right
# using _left_ for both sides here lets us benefit from an index on that column if one exists
])
end
# Returns a set of all of its children and nested children
def descendants
without_self self_and_descendants
end
分享到:
相关推荐
gem 'awesome_nested_set' 用法 要使用awesome_nested_set您的模型需要具有3个字段: lft , rgt和parent_id 。 这些字段的名称是可配置的。 您还可以具有可选字段: depth和children_count 。 这些字段是可配置的...
Javascript Awesome Packages 内容 :vertical_traffic_light: 国家管理 :droplet: 测验 :dvd: 邦德勒 :hammer_and_wrench: 实用工具 :globe_with_meridians: 浏览器 :rainbow: 设计系统 :chart_increasing_...
与和awesome_nested_set一起使用。 奖励功能: 对带有rails_admin_toggleable的已enabled列的特殊情况支持 cover和image栏的特殊情况支持 安装 将此行添加到应用程序的Gemfile中: gem 'rails_admin_nested_set...
awesome-blackmagic::performing_arts::heart_suit:奇技淫巧:diamond_with_a_dot:黑魔法大集合:diamond_suit::club_suit:
用于在每种类型的Android设备上构建Awesome应用程序的工具。 -代码编辑。 重新定义。 :handshake: 显示一些支持 如果你 :thumbs_up: 这个项目,别忘了 :star: 敬请关注最新动态 :call_me_hand: :bug: 错误/请求 ...
令人敬畏的情感分析:cur情感分析方法,实现和杂项的精选清单。 :sad_but_relieved_face::worried_face::face_screaming_in_fear::face_with_steam_from_nose:
这个宝石取决于 CollectiveIdea 的 Awesome Nested Set gem。 如果安装此 gem 时尚未存在,则会安装它。 你可以在 GitHub 上的上找到这个 gem 安装 在您的 Gemfile 中,添加: gem 'acts_as_commentable_with_...
java7集合源码解析灵感来自 算法和数据结构 名称 描述 星数 :books: :chart_increasing: C#标准数据结构与算法即插即用类库项目 3368 .NET ...包含复杂的算法和数据结构,如图形、优先级队列、命令、撤消-重做等。...
颜色分类leetcode 很棒的 Tensorflow 实现 非常简单的 TensorFlow 示例 代码: 序列到序列——视频到文本 Subhashini Venugopalan、Marcus Rohrbach、Jeff Donahue、Raymond Mooney、Trevor Darrell、Kate Saenko,...
:koala: :awesome- go-精选的Go框架,库和软件的精选列表 :elephant: :perst-在任何现有或新的Postgres应用程序上简化并加速开发,即时,实时,高性能的开发 :memo: :vim-bootstrap- generator提供了一种为vim...
sip构建java源码很棒的语音 :red_heart: 支持我的应用 :red_heart: :red_heart: :red_heart: :smiling_face_with_halo: :smiling_face_with_heart-eyes: :sign_of_the_horns: :red_heart: :red_heart: ...
:party_popper:Life is fantastic:partying_face:!~ “您知道您所有的门都被锁了吗? ”-里迪克(里迪克纪事) 由创建 :two_hearts: 网站登录模型 ...关于 模拟登陆基本采用的是Selenium+ webdriver的方式,有的网站...
awesome-java-security:令人敬畏的Java安全资源:sunglasses::hot_beverage::locked_with_key:
iya Waka自述文件 :cat_face: 我的Github数据 :trophy: 2021年有425项贡献 :package: 25.0 kB用于Github的存储 :briefcase: 选择雇用 :scroll: 36个公共存储库 :key: 2个私有存储库我很早 :baby_chick: :sun_with_...
Portfólio Bem-vindo ao projeto do meuportfólio:man_technologist_light_skin_tone: :briefcase: ·报告程序· nd迪斯 原始的灵魂 :information: 住宅的外观设计和外观设计,以及房屋装修。...
初步回归法matlab代码很棒的R 精选的R软件包和工具的精选列表。 受启发。 要获得更好的导航,请参阅 用于CRAN下载的软件包或超过400个的仓库 2018年 -D3可视化的R接口 -教学课程列表R -基于承诺的异步编程的抽象 ...
网站开发资源 Web开发人员的有用资源列表! 将其放在您的书签中并做出贡献 :red_heart: 你喜欢它? :star: 并与友好的Web开发人员! 你不喜欢吗? :dog_face: 最初由在创建。 文件 ... :graduation_
学习C ++面向对象编程-0 to Hero :male_sign::male_sign: 1979年由Bjarne Stroustrup发行的《不属于任何人的节目》。 连续性补充剂和补充剂。 西班牙语,蓬塔维斯塔德洛斯瓜纳斯州东方人和东方遗物,C ++ es ...
awesome-bots:关于机器人的最棒列表:white_medium_star::robot_face:
:waving_hand: 很棒的普罗米修斯警报 大多数警报规则对于每个Prometheus设置都是通用的。 我们需要一个找到所有地方的地方。 :sign_of_the_horns: :police_car_light: :bar_chart: 可在此处收集: : ...