Could you explain in detail what the :before_save and :before_create Ruby on Rails callbacks are, and what they have to do with Rails validations? Does validation occur after :before_save or :before_create?
Could you explain in detail what the :before_save and :before_create Ruby on Rails callbacks
Share
In a create operation under Rails, there are six callbacks before the database operation, and two after. In order, these are:
before_validationbefore_validation_on_createafter_validationafter_validation_on_createbefore_savebefore_createDATABASE INSERT
after_createafter_saveUpdate operations have exactly the same set, except read
updateinstead ofcreateeverywhere (and UPDATE instead of INSERT).From this, you can see that validation is carried out before the
before_saveandbefore_createcallbacks.The
before_saveoccurs slightly before thebefore_create. To the best of my knowledge, nothing happens between them; butbefore_savewill also fire on Update operations, whilebefore_createwill only fire on Creates.