Active Record Callbacks - Ruby on Rails Guides - 0 views
-
Active Record provides hooks into this object life cycle so that you can control your application and its data.
- ...42 more annotations...
-
after_initialize callback will be called whenever an Active Record object is instantiated, either by directly using new or when a record is loaded from the database
-
should be used with caution, however, because important business rules and application logic may be kept in callbacks.
-
As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate
-
When using the :if option, the callback won't be executed if the predicate method returns false; when using the :unless option, the callback won't be executed if the predicate method returns true.
-
needed to instantiate a new PictureFileCallbacks object, since we declared our callback as an instance method.
-
Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.
-
very similar to the after_save callback except that they don't execute until after database changes have either been committed or rolled back
-
If anything raises an exception after the after_destroy callback is called and the transaction rolls back, the file will have been deleted and the model will be left in an inconsistent state
-
-
The after_commit and after_rollback callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block.