Ben Stephens
2007-Jun-12 08:18 UTC
How do I get act_as_tree members to use the same object for their parent?
Hi, how do I get act_as_tree members to use the same object for their parent? For example at the moment I have: tree_member = TreeMember.find(8) sibling_tree_member = TreeMember.find(9) tree_member.parent.id => 2 sibling_tree_member.parent.id => 2 tree_member.parent.object_id => 37862220 sibling_tree_member.parent.object_id => 37850860 Regards, Ben --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
MichaelLatta
2007-Jun-12 15:30 UTC
Re: How do I get act_as_tree members to use the same object for their parent?
Rails does not guarantee that all reads of a row result in the same Ruby object. When you traverse a relationship it will re-read the row if that object has not traversed that relationship previously. In your case the .parent relationship is being traversed from each child object so you get 2 ruby objects for the same row in the database. If you find all rows with parent == nil and walk down the tree you may be able to ensure that there is only one ruby object for each row. But, it may only mean that the children relationship is cached. This is the biggest downside of AR and other thin layer OR layers. But, it also has advantages in that you can work with record sets that are distinct in purpose even though they happen to include the same records. Michael On Jun 12, 1:18 am, "Ben Stephens" <foolfod...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, how do I get act_as_tree members to use the same object for their parent? > > For example at the moment I have: > > tree_member = TreeMember.find(8) > sibling_tree_member = TreeMember.find(9) > > tree_member.parent.id > => 2 > > sibling_tree_member.parent.id > => 2 > > tree_member.parent.object_id > => 37862220 > > sibling_tree_member.parent.object_id > => 37850860 > > Regards, > Ben--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---