This project is archived and is in readonly mode.
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
end
class Member < ActiveRecord::Base
belongs_to :member_type
has_many :member_detail
end
class MemberDetail < ActiveRecord::Base
belongs_to :member
has_one :member_type, :through => :member
end
assert_not_nil @member_detail.member_type(true) # => #<MemberDetail ...
@member_detail.destroy
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
-
John Hawthorn November 22nd, 2010 @ 10:35 PM
Here is my attempt at a patch
It adds foreign_key_present to HasOneThroughAssociation which returns true when the through_reflection is a belongs_to and primary_key is not nil. Also adds test cases.
-
John Hawthorn November 29th, 2010 @ 04:13 AM
It seems that the commit which caused this issue (https://github.com/rails/rails/commit/f1c13b0dd7b22b5f6289ca1a09f1d...) has been reverted (https://github.com/rails/rails/commit/e444439fe28f873c783a4b99b1c9f....
I've attached a new patch which contains only the test case.
-
Aaron Patterson November 29th, 2010 @ 05:00 AM
- State changed from new to open
- Milestone cleared.
- Assigned user set to Aaron Patterson
- Importance changed from to Low
-
Repository November 29th, 2010 @ 09:54 AM
- State changed from open to resolved
(from [28896a9f4ae0830726619bc479f69263acb80e4b]) test case on has_one :through after a destroy
[#6037 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/28896a9f4ae0830726619bc479f69... -
Repository November 29th, 2010 @ 09:56 AM
(from [52e854e90025edfcf2c4665b81b17d3cd840f9d9]) test case on has_one :through after a destroy
[#6037 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/52e854e90025edfcf2c4665b81b17...
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
Attachments
Referenced by
- 6037 has_one :through a belongs_to association will return nil on deleted records [#6037 state:resolved]
- 6037 has_one :through a belongs_to association will return nil on deleted records [#6037 state:resolved]