How best to extend acts_as_tree?

I’ve got a bunch of tables that will act_as_tree. I don’t see a method
(maybe I’m missing it) to “get myself and my children”? So I wanted to
add it to my model, but I have so many acts_as_tree models I was trying
to figure out a way to write my self_and_children method once, but I’m
unfamiliar with extending the existing code in this way. Can somebody
point me in the right direction? Thanks! (This code is for a rapid
prototype, so scale and performance are not crucial issues, I’m just
can’t bring myself to cut and paste the same method over and over
again.)

I’ve just realized that if an object’s parent_id is set equal to itself,
then it will include itself among it’s children. Perhaps that’s all I
need, at least for my purposes right now. Sorry for the interruption.

Duane M. wrote:

I’ve got a bunch of tables that will act_as_tree. I don’t see a method
(maybe I’m missing it) to “get myself and my children”? So I wanted to
add it to my model, but I have so many acts_as_tree models I was trying
to figure out a way to write my self_and_children method once, but I’m
unfamiliar with extending the existing code in this way. Can somebody
point me in the right direction? Thanks! (This code is for a rapid
prototype, so scale and performance are not crucial issues, I’m just
can’t bring myself to cut and paste the same method over and over
again.)

If you want the object and it’s children all in a 1D array:

def self_and_children
[self] + children
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs