Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format, and many more.
9 Callback Classes You can declare as many callbacks as you want inside your callback classes.
Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
The most-up-to-date version of this list for the latest version of Rails can be found in the ActiveRecord::Callbacks documentation. The lists for Rails 4, 3 & 2 are below.
The most up-to-date version of this list can be found in the Rails 4 Guides.
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_saveafter_commit/after_rollbackbefore_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_saveafter_commit/after_rollbackbefore_destroyaround_destroyafter_destroyafter_commit/after_rollbackThe most up-to-date version of this list can be found in the Rails 3 Guides.
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_savebefore_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_savebefore_destroyaround_destroyafter_destroyThe most up-to-date version of this list can be found in the Rails 2.3 Guides
before_validationbefore_validation_on_createafter_validationafter_validation_on_createbefore_savebefore_createINSERT operationafter_createafter_savebefore_validationbefore_validation_on_updateafter_validationafter_validation_on_updatebefore_savebefore_updateUPDATE operationafter_updateafter_savebefore_destroyDELETE operationafter_destroySince you need to first validate the reference_code, the assign_reference method can be called in the after_validation callback or any callback appearing after it in the list I provided above.
Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_saveafter_commit/after_rollbackbefore_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_saveafter_commit/after_rollbackbefore_destroyaround_destroyafter_destroyafter_commit/after_rollbackafter_save runs both on create and update, but always after the more specific callbacks after_create and after_update, no matter the order in which the macro calls were executed.
before_destroy callbacks should be placed before dependent: :destroy associations (or use the prepend: true option), to ensure they execute before the records are deleted by dependent: :destroy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With