Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@wycats wycats authored
View
19 activerecord/lib/active_record/autosave_association.rb
@@ -140,6 +140,23 @@ def #{type}(name, options = {})
CODE
end
+ def define_non_cyclic_method(name, reflection, &block)
+ define_method(name) do |*args|
+ result = true; @_already_called ||= {}
+ # Loop prevention for validation of associations
+ unless @_already_called[[name, reflection.name]]
+ begin
+ @_already_called[[name, reflection.name]]=true
+ result = instance_eval(&block)
+ ensure
+ @_already_called[[name, reflection.name]]=false
+ end
+ end
+
+ result
+ end
+ end
+
# Adds validation and save callbacks for the association as specified by
# the +reflection+.
#
@@ -169,7 +186,7 @@ def add_autosave_association_callbacks(reflection)
define_method(save_method) { save_has_one_association(reflection) }
after_save save_method
else
- define_method(save_method) { save_belongs_to_association(reflection) }
+ define_non_cyclic_method(save_method, reflection) { save_belongs_to_association(reflection) }
before_save save_method
end
end
View
12 activerecord/test/cases/autosave_association_test.rb
@@ -3,8 +3,10 @@
require 'models/company'
require 'models/customer'
require 'models/developer'
+require 'models/face'
require 'models/invoice'
require 'models/line_item'
+require 'models/man'
require 'models/order'
require 'models/parrot'
require 'models/person'
@@ -880,6 +882,16 @@ def test_should_not_load_the_associated_model
end
end
+class TestAutosaveInverseAssociationOnAHasOneAssociation < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def test_should_save_the_inverse_association_model
+ man = Man.new
+ man.build_face
+ man.face.save
+ end
+end
+
class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
self.use_transactional_fixtures = false
View
2  activerecord/test/models/face.rb
@@ -4,4 +4,6 @@ class Face < ActiveRecord::Base
# These is a "broken" inverse_of for the purposes of testing
belongs_to :horrible_man, :class_name => 'Man', :inverse_of => :horrible_face
belongs_to :horrible_polymorphic_man, :polymorphic => true, :inverse_of => :horrible_polymorphic_face
+
+ accepts_nested_attributes_for :man
end
Please sign in to comment.
Something went wrong with that request. Please try again.