Tyler Blatt
Tyler Blatt

Reputation: 135

after_create called after failed saved?

I want to send request to external software whenever I am certain that a model is saved.

1) How do I determine the order in which after_create is called?

2) does it get called on a failed create?

The RAILS API documentation says:

Note that this callback is still wrapped in the transaction around save.

3) Does this mean it requires the save to be successful before after_save is called or does it mean that once save is called after_save is always triggered?

Upvotes: 9

Views: 2552

Answers (1)

Karl Wilbur
Karl Wilbur

Reputation: 6217

How would I learn in what order after_create is called and if it persists through a failed create?

You can read the order of callbacks here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

If the transaction fails, the save event (and create/update events) return false. This will stop all later callbacks from being run. So, after_save, after_create/after_update all never get run on a failed save (or on a failed create/update).

...and the transaction gets rolled back so the database is not actually updated.

Upvotes: 6

Related Questions