Prev | Current Page 97 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

find :all, :include => {:children => {}},
:conditions => ["children_people.name = ?", name]
If Active Record finds that it??™s trying to join back to a table it has already included, it will
change the name of the table to eliminate ambiguity. It does this using a breadth-first search
of all the keys in the hash table, so in the rather contrived example of
:include => { :parent => { :parent => {}}, :children => {}}
the parent can be referenced via parents_people, but the parent??™s parent must be referenced
via parents_people_2. The full method call for obtaining all the people whose grandparents
have a specific name is
Person.find :all, :include => { :parent => { :parent => {} },
:conditions => ["parents_people_2.name = ?", name ]
This would generate the following SQL clause:
SELECT *
FROM people
LEFT OUTER JOIN people AS parents_people
ON parents_people.id = people.person_id
LEFT OUTER JOIN people AS parents_people_2
ON parents_people_2.id = parents_people.person_id
When there are no includes specified??”so there is only one table involved in the query??”
the SELECT clause is an asterisk (*) for simplicity.


Pages:
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109