11 MarActiveRecord lifecycle

Thursday, 11 March 2010 — 16:24

Most rails developers know the language provides us with callbacks that allow us to run some logic before the ActiveRecord object is saved or updated. However, not everyone knows where to place this logic in the ActiveRecord cycle. This is the execution order and available callbacks, for a save/update instruction:

save
valid
(1) before_validation
(2) before_validation_on_create/ on_update
validate
validate_on_create/on_update
(3) after_validation
(4) after_validation_on_create/on_update
(5) before_save/before_update
(6) before_create

Very frequent bugs consist of making changes (that might break validations) on the model making use of before_save callback. If you do so, you might be filling your database with non valid records, as validations will not be run. Use before_validation in those cases

Ger

Comentarios

Añade tu comentario




(textile avilitado)
Negrita: *Google*
Enlace: "google.com":http://www.google.com
Imagen: !http://vicentgozalbes.com/images/avatar.png!

ó Cancelar