This project is archived and is in readonly mode.

#4158 ✓invalid
Greg Hazel

has_one and belongs_to loading too much

Reported by Greg Hazel | March 12th, 2010 @ 10:38 AM

class User < ActiveRecord::Base
  has_one :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end
?> current_user.profile.user.profile.user.profile.user.profile
  User Load (0.4ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4)
  Profile Load (0.1ms)   SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4) LIMIT 1
  User Load (0.1ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4)
  Profile Load (0.1ms)   SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4) LIMIT 1
  User Load (0.1ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4)
  Profile Load (0.1ms)   SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4) LIMIT 1

This one-liner is obviously just an demonstration of the issue. The actual situation this was discovered in was a few user methods which call methods on the profile which call methods on the user, etc.

The reflections could be aware of the symmetry and connect the two objects, preventing further loading.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages