Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

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

#2185

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)

Reported by Chinasaur · March 9th, 2009 @ 08:25 PM

State: stale
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Chinasaur
    Chinasaur
    • Assigned user set to Frederick Cheung

    Though FC might be the person for this...

    March 9th, 2009 @ 08:34 PM

  2. Chinasaur
    Chinasaur
    • 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.

    March 13th, 2009 @ 02:35 PM

  3. Ryan Bigg
    Ryan Bigg
    • State changed from new to open
    • Assigned user changed from Frederick Cheung to Pratik

    April 29th, 2010 @ 10:30 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:53 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:53 PM