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.

belongd_to proxy loads from database every time if record is missing

#2961

Greetings all,

I'm hitting some interesting behaviour in the association proxy code that seems to be intentional, but seems non-optimal and is actually causing quite a bit of unnecessary DB querying in our case.

Basically it looks like there is some special-casing of the BelongsToAssociation proxy to avoid setting its target to nil. In particular there are these lines in association.rb:

define_method(reflection.name) do |*params|
  force_reload = params.first unless params.empty?
  association = association_instance_get(reflection.name)

  if association.nil? || force_reload
    association = association_proxy_class.new(self, reflection)
    retval = association.reload
    if retval.nil? and association_proxy_class == BelongsToAssociation
      association_instance_set(reflection.name, nil)
      return nil
    end
    association_instance_set(reflection.name, association)
  end

  association.target.nil? ? nil : association
end

I can understand not returning the proxy when the target is nil, since the proxy can't impersonate nil. However, not setting the proxy into the instance variable means the DB will be requeried every time for no good reason, and it seems to me that assigning the proxy will still have the correct behaviour from the model's perspective. The same thing happens on preloading, meaning that preloading doesn't actually do anything for missing records.

Does anyone know the reason for this behaviour? I'm using Rails 2.3.2. Here's a reproduction of the problem from the application's perspective.

class Alpha < ActiveRecord::Base
end

class Beta < ActiveRecord::Base
  belongs_to :alpha
end

a = Alpha.create
b = Beta.create :alpha => a
a.destroy

b_loaded = Beta.find(b.id, :include => :alpha)
b_loaded.loaded_alpha?    # => false (or nil, rather, but not true)
b_loaded.alpha            # => nil
b_loaded.loaded_alpha?    # => still nil rather than true
b_loaded.alpha            # => nil (and hits the DB again)

Thanks!

Reported by Robin Salkeld · July 27th, 2009 @ 10:18 PM

State: stale
Milestone: 3.x
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. Robin Salkeld
    Robin Salkeld

    Whoops, didn't format the example correctly. Sorry - I'm a lighthouse noob. :)

    class Alpha < ActiveRecord::Base end

    class Beta < ActiveRecord::Base

    belongs_to :alpha
    

    end

    a = Alpha.create b = Beta.create :alpha => a a.destroy

    b_loaded = Beta.find(b.id, :include => :alpha) b_loaded.loaded_alpha? # => false (or nil, rather, but not true) b_loaded.alpha # => nil b_loaded.loaded_alpha? # => still nil rather than true b_loaded.alpha # => nil (and hits the DB again)

    July 27th, 2009 @ 10:19 PM

  2. Robin Salkeld
    Robin Salkeld

    Whoops, didn't format the example correctly. Sorry - I'm a lighthouse noob. :)

    class Alpha < ActiveRecord::Base end

    class Beta < ActiveRecord::Base

    belongs_to :alpha
    

    end

    a = Alpha.create b = Beta.create :alpha => a a.destroy

    b_loaded = Beta.find(b.id, :include => :alpha) b_loaded.loaded_alpha? # => false (or nil, rather, but not true) b_loaded.alpha # => nil b_loaded.loaded_alpha? # => still nil rather than true b_loaded.alpha # => nil (and hits the DB again)

    July 27th, 2009 @ 10:19 PM

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

    May 4th, 2010 @ 06:48 PM

  4. Dan Pickett
    Dan Pickett
    • Assigned user set to Ryan Bigg

    Robin,

    Could you provide a failing test case that verifies this errant behavior?

    Ryan - assigning to you in order to mark this as stale or incomplete.

    May 9th, 2010 @ 06:55 PM

  5. Ryan Bigg
    Ryan Bigg
    • State changed from new to incomplete

    May 9th, 2010 @ 10:16 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open
    • 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:39 PM

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

    February 2nd, 2011 @ 04:39 PM