Permalink
Browse files

Bugfix circular reference while saving has_one relationship

  • Loading branch information...
1 parent c67ff97 commit a97cf753a116749ed307546e72eba4c1d4c4361f @AntiTyping committed Feb 15, 2012
Showing with 18 additions and 1 deletion.
  1. +18 −1 activerecord/lib/active_record/autosave_association.rb
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

0 comments on commit a97cf75

Please sign in to comment.