aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb6
2 files changed, 7 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 1ba537dce0..e49e96e738 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed saving of in-memory association structures to happen as a after_create/after_update callback instead of after_save -- that way you can add new associations in after_create/after_update callbacks without getting them saved twice
+
* Allow any Enumerable, not just Array, to work as bind variables #1344 [Jeremy Kemper]
* Added actual database-changing behavior to collection assigment for has_many and has_and_belongs_to_many #1425 [Sebastian Kanthak].
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 65549aca0c..610f1e3667 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -671,7 +671,7 @@ module ActiveRecord
end
module_eval do
- after_save <<-end_eval
+ after_callback = <<-end_eval
association = instance_variable_get("@#{association_name}")
if association.respond_to?(:loaded?)
if @new_record_before_save
@@ -683,6 +683,10 @@ 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)
end
end