:id)" I got t" /> :id)" I got t" /> :id)" I got t"/>

Rails 3 - Ancestry gem method arrange not working

1.9k Views Asked by At

hi people

I've follow this tutorial: http://railscasts.com/episodes/262-trees-with-ancestry

when I tried to make the "arrange (:order => :id)" I got this message

    undefined method `arrange' for #<Array:0x8c19780>

Extracted source (around line #4):

1: 
2: <h1>Listing categories</h1>
3: 
4: <%= nested_categories @categories.arrange(:order => :id) %>
5: 
6: <br />
7:

Can you help me, please?

Thanks!

3

There are 3 best solutions below

1
Tilo On BEST ANSWER

I'm using the ancestry Gem , and it works fine for me. e.g.:

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

You need to run it on your Categories class itself -- it's a class method

try it out in the Rails console!

0
User 1058612 On

I'll clarify @Tilo's answer:

In your Controller, do:

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

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

And in your View, do:

<%= nested_categories @categories %>

I'm using Rails 3, and the "Ancestry" gem seems like a great replacement for the "Acts As Tree" gem.

0
Whyves On

As I have described in this post ...

The arrange() method acts either at the class level or on a named scope, not on a returned array of values. For example, if you want to call arrange() on a table called 'Activity', this would work and return the entire table in an arranged format:

Activity.arrange

However, if you want to call arrange on a specific tree in the table you would go:

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

'subtree' is a named scope provided by the has_ancestry gem. Therefore, the arrange() method should work for the following named scope:

  • ancestors
  • children
  • sibblings
  • descendants
  • subtree