DAZ wrote:
> On Dec 28, 8:07 pm, Mark Reginald James <[email protected]> wrote:
>
>> One alternative is to make the ancestor array a string key
>> to each record ("abe|homer|bart"), allowing instant retrieval.
>
> This seems like a relatively good idea, could have a string-key called
> family_tree or something and just do find_by_family_tree("abe|homer|
> bart")
> This doesn't quite feel right - it seems like the only info you should
> need to keep is a person's parent (from which you can then find their
> parent and so forth). It might also lead to some very long strings
> eventually!
It does require maintenance: whenever a name changes you have to
update the string in all children and all ancestors.
>> Another would be to build the sql iteratively:
>>
>
> This is what I'm doing at the moment - I'm using the "betternestedset"
> plugin, so have access to a "children" method that returns an arrary
> of children. Will this have all been pre-fetched efficiently? And if
> so, is the iterative code the way to do it?
>
> tree = ["abe","homer","bart"]
> person = Person.find_by_name(tree[0])
> if tree.size > 1
> 1.upto(tree.size - 1) do |i|
> person = person.children.find { |child| child.name == tree
> [i] }
> end
> end
> @person = person
First-generation children are efficiently retrieved in both
better_nested_set and acts_as_tree via the parent key.
better_nested_set also allows you to efficiently retrieve all
generations of children using the all_children method.
So your current method will be making one DB call per
generation. And you should put the name match into the
SQL conditions rather than in a Ruby loop.
But have a look at the code in my last post. It can find
the correct Bart using only one DB call by matching
up the unique ancestors chain.
--
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---