Permalink
Browse files

Merge pull request #9499 from dmitry/inverse_instance_should_not_be_r…

…eloaded_after_stale_state_change

Inversed instance should not be reloaded after stale state was changed

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
1 parent 1366df2 commit c2b10b753502fd54719f3dcdb98ac444493aa341 @rafaelfranca rafaelfranca committed Oct 14, 2013
@@ -1,5 +1,11 @@
## unreleased ##
+* Prevent the inversed association from being reloaded on save.
+
+ Fixes #9499.
+
+ *Dmitry Polushkin*
+
* `Relation#order` quotes the column name if you pass a `Symbol`.
Fixes #11870.
@@ -18,6 +18,7 @@ module Associations
# HasManyThroughAssociation + ThroughAssociation
class Association #:nodoc:
attr_reader :owner, :target, :reflection
+ attr_accessor :inversed
delegate :options, :to => :reflection
@@ -43,6 +44,7 @@ def reset
@loaded = false
@target = nil
@stale_state = nil
+ @inversed = false
end
# Reloads the \target and returns +self+ on success.
@@ -60,8 +62,9 @@ def loaded?
# Asserts the \target has been loaded setting the \loaded flag to +true+.
def loaded!
- @loaded = true
+ @loaded = true
@stale_state = stale_state
+ @inversed = false
end
# The target is stale if the target no longer points to the record(s) that the
@@ -71,7 +74,7 @@ def loaded!
#
# Note that if the target has not been loaded, it is not considered stale.
def stale_target?
- loaded? && @stale_state != stale_state
+ !inversed && loaded? && @stale_state != stale_state
end
# Sets the target of this association to <tt>\target</tt>, and the \loaded flag to +true+.
@@ -110,6 +113,7 @@ def set_inverse_instance(record)
if record && invertible_for?(record)
inverse = record.association(inverse_reflection_for(record).name)
inverse.target = owner
+ inverse.inversed = true
end
end
@@ -507,6 +507,18 @@ def test_child_instance_should_be_shared_with_replaced_via_method_parent
assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
end
+ def test_inversed_instance_should_not_be_reloaded_after_stale_state_changed
+ new_man = Man.new
+ face = Face.new
+ new_man.face = face
+
+ old_inversed_man = face.man
+ new_man.save!
+ new_inversed_man = face.man
+
+ assert_equal old_inversed_man.object_id, new_inversed_man.object_id
+ end
+
def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
i = interests(:llama_wrangling)
m = i.polymorphic_man

0 comments on commit c2b10b7

Please sign in to comment.