want to get a list of all the ids of something that acts_as_tree

127 Views Asked by At

I have a Category that can have Headers which are acts_as_tree. The structue could this be like:

class Category < ActiveRecord::Base
  has_many :headers
end

class Header < ActiveRecord::Base
  belongs_to :category
  acts_as_tree :order => :position
end

Category:
  Header
  Header
    Header
      Header
      Header
  Header

I'd like to extract all of the ids of the Headers for a Category.

Is there something that's done for me automatically? Or is there a simple way to traverse?

thx in advance

1

There are 1 best solutions below

2
On

First of all retrieve a category you need like:

@category = Category.find(params[:id])

then

ids = @category.headers.map(&:id)

This line will return the array of headers' ids for a special category