My quick schema:
Code:
create_table "authors", :force => true do |t|
t.column "my_parent_id", :integer
t.column "text", :string
end
create_table "groups", :force => true do |t|
t.column "my_parent_id", :integer
t.column "text", :string
end
create_table "my_parents", :force => true do |t|
t.column "text", :string
end
create_table "orders", :force => true do |t|
t.column "my_parent_id", :integer
t.column "text", :string
end
create_table "users", :force => true do |t|
t.column "my_parent_id", :integer
t.column "text", :string
end
Code:
@parent = MyParent::find(6)
Generates the following:
Code:
Processing TestController#index (for 127.0.0.1 at 2006-04-12 12:44:03) [GET]
Session ID: 9a3f41b5f8020da4dfe82c68e5489cf8
Parameters: {"action"=>"index", "controller"=>"test"}
[4;35;1mMyParent Load (0.003991)[0m [0mSELECT * FROM my_parents WHERE (my_parents.id = 6) LIMIT 1[0m
Rendering within layouts/application
Rendering test/index
[4;35;1mAuthor Load (0.187731)[0m [0mSELECT * FROM authors WHERE (authors.my_parent_id = 6) [0m
[4;35;1mGroup Load (0.002873)[0m [0mSELECT * FROM groups WHERE (groups.my_parent_id = 6) [0m
[4;35;1mOrder Load (0.003503)[0m [0mSELECT * FROM orders WHERE (orders.my_parent_id = 6) [0m
[4;35;1mUser Load (0.003082)[0m [0mSELECT * FROM users WHERE (users.my_parent_id = 6) [0m
Whereas this:
Code:
@parent = MyParent::find(6, :include => [:authors, :users, :orders, :groups])
Generates one DB call:
Code:
[4;36;1mMyParent Load Including Associations (0.056154)[0m [0;1mSELECT my_parents."id" AS t0_r0, my_parents."text" AS t0_r1, authors."id" AS t1_r0, authors."my_parent_id" AS t1_r1, authors."text" AS t1_r2, users."id" AS t2_r0, users."my_parent_id" AS t2_r1, users."text" AS t2_r2, orders."id" AS t3_r0, orders."my_parent_id" AS t3_r1, orders."text" AS t3_r2, groups."id" AS t4_r0, groups."my_parent_id" AS t4_r1, groups."text" AS t4_r2 FROM my_parents LEFT OUTER JOIN authors ON authors.my_parent_id = my_parents.id LEFT OUTER JOIN users ON users.my_parent_id = my_parents.id LEFT OUTER JOIN orders ON orders.my_parent_id = my_parents.id LEFT OUTER JOIN groups ON groups.my_parent_id = my_parents.id WHERE (my_parents.id = 6) [0m
Note, I removed all development server related queries from the log output.
Bookmarks