Problems with acts_as_tree

I have an acts_as_tree association in my application and for the most
part it’s working fine but I’m concerned specifically about one part
of the design. When I add children if it is the first child being
added I want to flag it as the first child object. I chose to do this
by overriding << so to add children to the parent object I do parent
<< child. This works fine now but if someone were to add to
object.children manually (parent.children << child) it wouldn’t keep
track of the first child. What’s the ruby way of handling this? I’m
thinking maybe I need to override the children method somehow but I’m
not sure. Thanks any advice would be appreciated. -Mike

It depends on how your app works. If you aren’t moving children at
all, you don’t have to do anything, because the IDs will keep the
children in order. Otherwise you could register a before_create or
before_save method that checks for other children in the database,
then sets the flag.

Krishna

You could create a before_save callback. Check to see if the parent
has any children already. If not, set the flag.

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