mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix two has_one :through errors
* Set the association target on assignment; * Reset target to nil on reset, rather than empty array. Signed-off-by: Michael Koziarski <michael@koziarski.com> [#895 state:committed]
This commit is contained in:
parent
6ec07e0737
commit
9dbde4f5cb
3 changed files with 15 additions and 3 deletions
|
@ -1271,10 +1271,9 @@ module ActiveRecord
|
|||
self.send(reflection.name, new_value)
|
||||
else
|
||||
association.replace(new_value)
|
||||
end
|
||||
|
||||
instance_variable_set(ivar, new_value.nil? ? nil : association)
|
||||
end
|
||||
end
|
||||
|
||||
define_method("set_#{reflection.name}_target") do |target|
|
||||
return if target.nil? and association_proxy_class == BelongsToAssociation
|
||||
|
|
|
@ -23,6 +23,10 @@ module ActiveRecord
|
|||
def find_target
|
||||
super.first
|
||||
end
|
||||
|
||||
def reset_target!
|
||||
@target = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -101,4 +101,13 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
|
|||
assert_equal clubs(:crazy_club), members[0].sponsor_club
|
||||
end
|
||||
|
||||
def test_uninitialized_has_one_through_should_return_nil_for_unsaved_record
|
||||
assert_nil Member.new.club
|
||||
end
|
||||
|
||||
def test_assigning_association_correctly_assigns_target
|
||||
new_member = Member.create(:name => "Chris")
|
||||
new_member.club = new_club = Club.create(:name => "LRUG")
|
||||
assert_equal new_club, new_member.club.target
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue