Difference between after create, after save and after commit in rails callbacks

Viewed 28

The difference between after_create, after_save and after_commit in Rails is that:

after_save -> is invoked when an object is created and updated.
after_commit -> is called on create, update and destroy.
after_create -> is only called when creating an object.

Is this the only difference among those, or are there any other major differences?

1 Answers

You are almost correct. Please read the following phrases too. The core difference between these callbacks is the event they are trigged on and the sequence they are triggered in.

  • After Commit: Triggers after the object is successfully created, updated, or destroyed.
  • After Create: Triggers only when the object is created. It triggers just before the data is committed to the database.
  • After Save: Triggers only when the object is updated or created. It triggers just before the data is committed to the database.
Related