From 1c881ca5bfc36689b4918edf497b38d24df7b57e Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Wed, 10 Oct 2007 06:45:13 +0000 Subject: Ensure that 'autosaving' works when associations aren't loaded [Bryan Helmkamp] References #8713 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7824 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/associations.rb') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 1a4a46eb3d..ab4e02f86c 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1072,12 +1072,15 @@ module ActiveRecord after_callback = <<-end_eval association = instance_variable_get("@#{association_name}") - if association.respond_to?(:loaded?) && association.loaded? - if @new_record_before_save - records_to_save = association - else - records_to_save = association.select { |record| record.new_record? } - end + records_to_save = if @new_record_before_save + association + elsif association.respond_to?(:loaded?) && association.loaded? + association.select { |record| record.new_record? } + else + [] + end + + if !records_to_save.blank? records_to_save.each { |record| association.send(:insert_record, record) } association.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id end -- cgit v1.2.3