読者です 読者をやめる 読者になる 読者になる

ECのウェブ担当者のメモ

ECサイトを運営管理している、WEB担当プログラマのメモ

スポンサーリンク

Awesome Nested Setのチートシート

f:id:jun9632:20170130164755p:plain

こちらRailsのカテゴリを階層で管理してくれるGem Awesome Nested Setです

github.com

そのチートシートです。

github.com

主に使いそうなところ

これで一気にループ回せます。

Category.each_with_level(Category.root.self_and_descendants) do |category, level|
  ...
end

使いそうなメソッド一覧

my_cat.root                  root for this node
my_cat.level                 the level of this object in the tree (e.g. root = 0)
my_cat.parent                the node's immediate parent
my_cat.children              array of immediate children (just those in the next level)
my_cat.ancestors             array of all parents, parents' parents, etc, excluding self
my_cat.self_and_ancestors    array of all parents, parents' parents, etc, including self
my_cat.siblings              array of brothers and sisters (all at that level), excluding self
my_cat.self_and_siblings     array of brothers and sisters (all at that level), including self
my_cat.descendants           array of all children, children's children, etc., excluding self
my_cat.self_and_descendants  array of all children, children's children, etc., including self
my_cat.leaves                array of all descendants that have no children
my_cat.root?                         true if this is a root node
my_cat.child?                        true if this is a child node (i.e. it has a parent)
my_cat.is_ancestor_of?(obj)          true if nested by any obj
my_cat.is_or_is_ancestor_of?(obj)    true if nested by any obj or self is obj
my_cat.is_descendant_of?(obj)        true if self is nested under obj
my_cat.is_or_is_descendant_of?(obj)  true if self is nested under obj or self is obj
my_cat.leaf?                         true if this is a leaf node (i.e. it has no children)

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Ruby on Rails 5 超入門

Ruby on Rails 5 超入門