This project is archived and is in readonly mode.

#3866 ✓duplicate
Fabio Yamate

Callbacks being called twice with nested attributes

Reported by Fabio Yamate | February 5th, 2010 @ 08:36 PM

With a blog project with post-comments, using rails 2.3.5:

class Post < ActiveRecord::Base
  has_many :comments
  
  accepts_nested_attributes_for :comments
  ...
end

class Comment < ActiveRecord::Base
  belongs_to :post
  ...
end

So, printing out all callbacks:

def callback_name
  p "#{callback_name} - #{object_id}"
  ...
end

each callback of comments is called twice, including save.

"post_before_validation - 2174878780"
"post_before_validation_on_update - 2174878780"
"comment_before_validation - 2174811020"
"comment_before_validation_on_update - 2174811020"
"comment_after_validation - 2174811020"
"comment_after_validation_on_update - 2174811020"
"comment_before_validation - 2174811020"
"comment_before_validation_on_update - 2174811020"
"comment_after_validation - 2174811020"
"comment_after_validation_on_update - 2174811020"
"post_after_validation - 2174878780"
"post_after_validation_on_update - 2174878780"
"post_before_save - 2174878780"
"post_before_update - 2174878780"
"comment_before_save - 2174811020"
"comment_before_update - 2174811020"
"comment_after_update - 2174811020"
"comment_after_save - 2174811020"
"comment_before_save - 2174811020"
"comment_before_update - 2174811020"
"comment_after_update - 2174811020"
"comment_after_save - 2174811020"
"post_after_update - 2174878780"
"post_after_save - 2174878780"

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages