谁为awesome_nested_set使用实例方法

时间:2015-10-08 07:33:04

标签: ruby-on-rails gem instance

我在ROR中实现了awesome_nested_set gem,它有实例方法(https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet)。我试过这个

Category.level(1)

但它显示错误

undefined method `level' 

这也不起作用

@sa = Category.siblings('name'=>'new')

     abort(@sa.siblings.inspect)

我该如何实现这些

3 个答案:

答案 0 :(得分:0)

似乎level是实例,而不是类方法。

所以,

category = Category.create!(bla-bla-bla)
puts category.label # this shoud work

答案 1 :(得分:0)

level是一个实例方法

它在备忘单here

中说明了

实例方法

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

因此请使用Category

实例的级别

答案 2 :(得分:0)

为awesome_nested_set

使用实例方法
cat = Category.find_by_name("here")
subcats = cat.ancestors
abort(subcats.inspect)