2011-10-18 57 views

回答

4

我使用祖先宝石,它对我来说工作正常。例如: -

require 'awesome_print'     # just for nice demo output 
ap Account.arrange(:order => :id) ; 1 

您需要在您的类别类本身运行它 - 这是一个类的方法

尝试一下在Rails的控制台!

+0

左右,railscast是错误的? ?...我必须更改代码中的其他任何内容,因为我完成了相同的步骤。在模型或控制器中,我应该在哪里编写你给我的代码作为例子? – Angelo

+0

已经解决了,谢谢 – Angelo

3

我会澄清@的Tilo的回答是:

在你的控制,这样做:

def index 
    @categories = Category.arrange(:order => :created_at) 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @categories } 
    end 
end 

而且在您看来,这样做:

<%= nested_categories @categories %> 

我使用Rails 3,而“Ancestry”宝石似乎是“Acts As Tree”宝石的重要替代品。

1

正如我在this post已经描述...

的安排()方法充当或者在类级别或在名为范围,而不是值的返回的数组。例如,如果你想打电话安排()上一个名为“活动”表,这会工作,并以排列格式返回整个表:

Activity.arrange 

不过,如果你想打电话安排在特定的树你会去:

Activity.find(...).subtree.arrange 

'子树'是由has_ancestry gem提供的命名范围。因此,安排()方法应该为以下命名范围工作:

  • 祖先
  • 孩子
  • sibblings
  • 后裔