aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb6
-rwxr-xr-xactiverecord/test/associations_test.rb10
3 files changed, 14 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 30dd70df25..c3ee4d5ff3 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Save associated records only if the association is already loaded. #8713 [blaine]
+
* MySQL: fix show_variable. #8448 [matt, Jeremy Kemper]
* Fixtures: correctly delete and insert fixtures in a single transaction. #8553 [Michael Schuerig]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index d30756b9c7..18f4580f45 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1100,8 +1100,8 @@ module ActiveRecord
after_callback = <<-end_eval
association = instance_variable_get("@#{association_name}")
-
- if association.respond_to?(:loaded?)
+
+ if association.respond_to?(:loaded?) && association.loaded?
if @new_record_before_save
records_to_save = association
else
@@ -1111,7 +1111,7 @@ module ActiveRecord
association.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id
end
end_eval
-
+
# Doesn't use after_save as that would save associations added in after_create/after_update twice
after_create(after_callback)
after_update(after_callback)
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index f009c4348e..52abc20e6e 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -69,7 +69,7 @@ class AssociationsTest < Test::Unit::TestCase
end
class AssociationProxyTest < Test::Unit::TestCase
- fixtures :authors, :posts, :categorizations, :categories
+ fixtures :authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects
def test_proxy_accessors
welcome = posts(:welcome)
@@ -98,6 +98,14 @@ class AssociationProxyTest < Test::Unit::TestCase
assert david.categories.include?(categories(:technology))
end
+ def test_save_on_parent_does_not_load_target
+ david = developers(:david)
+
+ assert !david.projects.loaded?
+ david.update_attribute(:created_at, Time.now)
+ assert !david.projects.loaded?
+ end
+
end
class HasOneAssociationsTest < Test::Unit::TestCase