In a model set to act as a tree, only the second of the functions below works as expected. The first returns an empty array. Why does the id entry have to be fetched from the database for the children function to work? It''s slower and forces me to create a root entry. Thanks. def self.get_children( id ) self.new( :id => id ).children end def self.get_children( id ) find(id).children end
Mark Reginald James
2005-Aug-30 13:10 UTC
Re: Finding children in a model acting as a tree
Mark Reginald James wrote:> In a model set to act as a tree, only the second of the functions > below works as expected. The first returns an empty array. Why > does the id entry have to be fetched from the database for the > children function to work? It''s slower and forces me to create > a root entry. Thanks. > > def self.get_children( id ) > self.new( :id => id ).children > end > > def self.get_children( id ) > find(id).children > endI realise now that setting a foreign key attribute in a model instance does not automatically instantiate its corresponding table association attribute. This is only done for instances that come from the database. To me this in non-intuitive. Is it done for a reason? -- We develop, watch us RoR, in numbers too big to ignore.