ActiveRecord, which comes first?

A simple question is really in the processing order that will happen first when a new item is saved.

  • :before_create
  • :validates
+4
source share
4 answers

validates comes first. From docs :

 (-) save (-) valid (1) before_validation (-) validate (2) after_validation (3) before_save (4) before_create (-) create (5) after_create (6) after_save (7) after_commit 
+14
source

before_create not called until the object is valid, so confirmation is checked first.

+2
source

from http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

 (-) save (-) valid (1) before_validation (-) validate (2) after_validation (3) before_save (4) before_create (-) create (5) after_create (6) after_save (7) after_commit 
+2
source

According to docs :

(-) save

(-) valid

(1) before_validation

(-) check

(2) after_validation

(3) before_save

(4) before_create

(-) create

(5) after_create

(6) after_save

(7) after_commit

+1
source

All Articles