DAZ wrote:
> I have a family-tree structure to a person model.
>
> I want to be able to find people by specifying an array that
> corresponds to the family tree. So if Abe is grandpa, homer is dad and
> bart is the son, bart's array would be
>
> To find bart, I can't just use find_by_name("bart") as there might be
> another person called bart (with differnt parents and therefore a
> different array). So, to find this particular bart, I want to find abe
> first, then find homer, then bart.
>
> How can I do an ititial database query of find_by_name("abe") that
> will find the "abe" record as well as all his descendant records, so
> that any subsequent filtering is efficient and doesn't hit the
> database?
Efficient retrieval of all descendants or all ancestors
of a record requires the addition of nested-set capability.
One alternative is to make the ancestor array a string key
to each record ("abe|homer|bart"), allowing instant retrieval.
Another would be to build the sql iteratively:
ancestors = %w(abe homer bart).reverse
conds = {"people.name" => ancestors.shift}
joins = nil
ancestors.each_with_index do |name, i|
joins = joins.nil? ? :parent : {:parent => joins}
conds.update("#parents_people#{'_' + (i+1).to_s if i!=0}.name" => name)
end
bart = Person.find(:first, :conditions => conds, :joins => joins)
--
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
-~----------~----~----~----~------~----~------~--~---