Skip to content
This repository
Browse code

Merge pull request #5044 from dracco/3-0-stable

Backport Bugfix: Stack Overflow (3-0-stable)
  • Loading branch information...
commit 51582feb0369ae33aedd1e5ce04bf892c2940a1b 2 parents f47a303 + 389d1c5
Yehuda Katz authored April 30, 2012
19  activerecord/lib/active_record/autosave_association.rb
@@ -140,6 +140,23 @@ def #{type}(name, options = {})
140 140
         CODE
141 141
       end
142 142
 
  143
+      def define_non_cyclic_method(name, reflection, &block)
  144
+        define_method(name) do |*args|
  145
+          result = true; @_already_called ||= {}
  146
+          # Loop prevention for validation of associations
  147
+          unless @_already_called[[name, reflection.name]]
  148
+            begin
  149
+              @_already_called[[name, reflection.name]]=true
  150
+              result = instance_eval(&block)
  151
+            ensure
  152
+              @_already_called[[name, reflection.name]]=false
  153
+            end
  154
+          end
  155
+
  156
+          result
  157
+        end
  158
+      end
  159
+
143 160
       # Adds validation and save callbacks for the association as specified by
144 161
       # the +reflection+.
145 162
       #
@@ -169,7 +186,7 @@ def add_autosave_association_callbacks(reflection)
169 186
               define_method(save_method) { save_has_one_association(reflection) }
170 187
               after_save save_method
171 188
             else
172  
-              define_method(save_method) { save_belongs_to_association(reflection) }
  189
+              define_non_cyclic_method(save_method, reflection) { save_belongs_to_association(reflection) }
173 190
               before_save save_method
174 191
             end
175 192
           end
12  activerecord/test/cases/autosave_association_test.rb
@@ -3,8 +3,10 @@
3 3
 require 'models/company'
4 4
 require 'models/customer'
5 5
 require 'models/developer'
  6
+require 'models/face'
6 7
 require 'models/invoice'
7 8
 require 'models/line_item'
  9
+require 'models/man'
8 10
 require 'models/order'
9 11
 require 'models/parrot'
10 12
 require 'models/person'
@@ -880,6 +882,16 @@ def test_should_not_load_the_associated_model
880 882
   end
881 883
 end
882 884
 
  885
+class TestAutosaveInverseAssociationOnAHasOneAssociation < ActiveRecord::TestCase
  886
+  self.use_transactional_fixtures = false
  887
+
  888
+  def test_should_save_the_inverse_association_model
  889
+    man = Man.new
  890
+    man.build_face
  891
+    man.face.save
  892
+  end
  893
+end
  894
+
883 895
 class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
884 896
   self.use_transactional_fixtures = false
885 897
 
2  activerecord/test/models/face.rb
@@ -4,4 +4,6 @@ class Face < ActiveRecord::Base
4 4
   # These is a "broken" inverse_of for the purposes of testing
5 5
   belongs_to :horrible_man, :class_name => 'Man', :inverse_of => :horrible_face
6 6
   belongs_to :horrible_polymorphic_man, :polymorphic => true, :inverse_of => :horrible_polymorphic_face
  7
+
  8
+  accepts_nested_attributes_for :man
7 9
 end

0 notes on commit 51582fe

Please sign in to comment.
Something went wrong with that request. Please try again.