How to determine if a record was created or updated in after_save callback (Ruby on Rails)

January 21, 2012 · Posted in Development 

sometimes in after_save callback it is needed to determine if a record was created or updated without the use of after_create callback.

Rails has no direct way for doing this.

The solution can be as follows:

def before_save
  @was_a_new_record = new_record?
  return true
end
 
def after_save
  if @was_a_new_record
    ...
  end
end

 

Comments