Permalink
Browse files

Merge remote-tracking branch 'kennyj/fix_5563'

Conflicts:
	activerecord/test/cases/associations/belongs_to_associations_test.rb
  • Loading branch information...
2 parents 98de70e + 365b8b6 commit 8b941e977c094bb2121f9eaef73f20392d20f581 @jonleighton jonleighton committed Apr 25, 2012
@@ -132,7 +132,8 @@ def target_scope
# ActiveRecord::RecordNotFound is rescued within the method, and it is
# not reraised. The proxy is \reset and +nil+ is the return value.
def load_target
- @target ||= find_target if find_target?
+ @target = find_target if (@stale_state && stale_target?) || find_target?
+
loaded! unless loaded?
target
rescue ActiveRecord::RecordNotFound
@@ -77,7 +77,7 @@ def target_id
end
def stale_state
- owner[reflection.foreign_key].to_s
+ owner[reflection.foreign_key] && owner[reflection.foreign_key].to_s
end
end
end
@@ -27,7 +27,8 @@ def raise_on_type_mismatch(record)
end
def stale_state
- [super, owner[reflection.foreign_type].to_s]
+ foreign_key = super
+ foreign_key && [foreign_key.to_s, owner[reflection.foreign_type].to_s]
end
end
end
@@ -62,7 +62,7 @@ def construct_join_attributes(*records)
# properly support stale-checking for nested associations.
def stale_state
if through_reflection.macro == :belongs_to
- owner[through_reflection.foreign_key].to_s
+ owner[through_reflection.foreign_key] && owner[through_reflection.foreign_key].to_s
end
end
@@ -717,4 +717,15 @@ def test_polymorphic_with_custom_primary_key
assert !citibank.association(:firm).stale_target?
end
+
+ def test_reflect_the_most_recent_change
+ author1, author2 = Author.limit(2)
+ post = Post.new(:title => "foo", :body=> "bar")
+
+ post.author = author1
+ post.author_id = author2.id
+
+ assert post.save
+ assert_equal post.author_id, author2.id
+ end
end

0 comments on commit 8b941e9

Please sign in to comment.