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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 9/30/07, mike <mikebannister-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could create a before_save callback. Check to see if the parent has any children already. If not, set the flag. On Sep 30, 6:43 pm, mike <mikebannis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---