0

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

timpone
  • 19,235
  • 36
  • 121
  • 211

1 Answers1

0

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

Sergey Kishenin
  • 5,099
  • 3
  • 30
  • 50
  • thx, Sergey. I tried this but the problem is that it doesn't descend the heirarchy for the headers that aren't top level. – timpone Oct 10 '12 at 06:11
  • The best thing you acn do is to use `ancestry` gem instead becaue `acts_as_tree` is very limited in functionality. Or you can write a monkey-patch like setting `category_id` for a child header according to its parent`s `category_id` value somewhere in `after_create` callback – Sergey Kishenin Oct 10 '12 at 06:28