This project is archived and is in readonly mode.
Rails 2.0.2 Bug: belongs_to association reader fails to call loaded on bad foreign keys
Reported by Chinasaur | March 9th, 2009 @ 08:25 PM | in 3.x
Hi, first time submitting a bug.
In at least Rails 2.0.2 through 2.3.2, belongs_to association_reader fails to call association.loaded on bad foreign keys. So for example:
class Contact < ActiveRecord::Base
belongs_to :company
end
c1 = Contact.new(:company_id => 1) # This exists
c2 = Contact.new(:company_id => 9287123987) # This doesn't exist
Benchmark.bm do |x|
x.report {500.times do; c1.company; end}
x.report {500.times do; c2.company; end}
end
Gives result:
user system total real
0.015000 0.000000 0.015000 ( 0.035000)
0.671000 0.359000 1.030000 ( 1.224000)
Of course, c2.company is nil, but c2.company should call association.loaded so that we don't go to the database 500 times to find nil 500 times.
I'm not sure where the call to association.loaded should come. Let me know if you need more information. Let me know if this has already been fixed.
Thanks!
Results on 2.2.2; still seems to be an issue:
r1 = Recipe.new(:category_id => 1) # This exists
r2 = Recipe.new(:category_id => 9287123987) # This doesn't exist
Benchmark.bm do |x|
x.report {1000.times do; r1.category; end}
x.report {1000.times do; r2.category; end}
x.report {1000.times do; r1.category(true); end}
x.report {1000.times do; r2.category(true); end}
end
user system total real
0.000000 0.000000 0.000000 ( 0.000000)
0.514000 0.203000 0.717000 ( 0.717000)
0.578000 0.234000 0.812000 ( 0.843000)
0.546000 0.156000 0.702000 ( 0.702000)
Results on 2.3.2; still seems to be an issue:
user system total real
0.000000 0.000000 0.000000 ( 0.000000)
0.531000 0.218000 0.749000 ( 0.749000)
0.671000 0.203000 0.874000 ( 0.874000)
0.592000 0.141000 0.733000 ( 0.795000)
Comments and changes to this ticket
-
Chinasaur March 9th, 2009 @ 08:34 PM
- Assigned user set to Frederick Cheung
Though FC might be the person for this...
-
Chinasaur March 13th, 2009 @ 02:35 PM
- Tag changed from active_record, associations, reload to active_record, associations, bugs, reload
Checked on 2.2.2 and it is still slow, although seems a bit better.
-
Ryan Bigg April 29th, 2010 @ 10:30 PM
- State changed from new to open
- Assigned user changed from Frederick Cheung to Pratik
-
Santiago Pastorino February 2nd, 2011 @ 04:53 PM
- Importance changed from to
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 04:53 PM
- State changed from open to stale
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>