This project is archived and is in readonly mode.

#6037 ✓resolved
John Hawthorn

has_one :through a belongs_to association will return nil on deleted records

Reported by John Hawthorn | November 22nd, 2010 @ 07:37 PM | in 3.0.5

This worked in rails 3.0.1 and now fails in rails 3.0.3 and in master. When a record is destroyed, attempting to load a has_one :through association on it will returning nil.

Here is an example test case

class MemberType < ActiveRecord::Base
class Member < ActiveRecord::Base
  belongs_to :member_type
  has_many :member_detail
class MemberDetail < ActiveRecord::Base
  belongs_to :member
  has_one :member_type, :through => :member
assert_not_nil @member_detail.member_type(true)        # => #<MemberDetail ...
assert_not_nil @member_detail.member.member_type(true) # => #<MemberDetail ...
assert_not_nil @member_detail.member_type(true)        # => nil

The issue is that AssociationProxy#load_target won't call find_target since @owner.persisted? and foreign_key_present are false.

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=""></a>