You should be able to rewrite this code - something like this? Of course, your real code probably has some additional complexity - ALSO: this code has not been verified.
Class Parent < ActiveRecord::Base has_many :children
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
A total of twelve callbacks that give you tremendous power to respond and prepare for each state in the Active Record life cycle. The sequence for calling Base # save for an existing record is the same, except that each _create callback is replaced by a corresponding _update callback.
Using
p = Parent.new(:foo => "bar") p.save p.children[0].name
ActiveRecord callbacks from rails console (with awesome_print ap)
> ap ActiveRecord::Callbacks::CALLBACKS [ [ 0] :after_initialize, [ 1] :after_find, [ 2] :after_touch, [ 3] :before_validation, [ 4] :after_validation, [ 5] :before_save, [ 6] :around_save, [ 7] :after_save, [ 8] :before_create, [ 9] :around_create, [10] :after_create, [11] :before_update, [12] :around_update, [13] :after_update, [14] :before_destroy, [15] :around_destroy, [16] :after_destroy, [17] :after_commit, [18] :after_rollback ]
source share