198

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?

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
Agung Prasetyo
  • 4,353
  • 5
  • 29
  • 37

3 Answers3

388

In a create operation under Rails, there are six callbacks before the database operation, and two after. In order, these are:

  1. before_validation
  2. before_validation_on_create
  3. after_validation
  4. after_validation_on_create
  5. before_save
  6. before_create

    DATABASE INSERT
  7. after_create
  8. after_save

Update operations have exactly the same set, except read update instead of create everywhere (and UPDATE instead of INSERT).

From this, you can see that validation is carried out before the before_save and before_create callbacks.

The before_save occurs slightly before the before_create. To the best of my knowledge, nothing happens between them; but before_save will also fire on Update operations, while before_create will only fire on Creates.

Vishal Nagda
  • 1,165
  • 15
  • 20
Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • 32
    [`before_validation_on_create` and `after_validation_on_create` are removed as of Rails 3](http://guides.rubyonrails.org/v3.2.9/active_record_validations_callbacks.html#available-callbacks), instead use `before_validation` and `after_validation` respectively with option `:on => :create`. – Sun May 20 '13 at 14:59
  • How would you refer to the record that was just created when using `after_save` or `after_create`? – bcackerman Nov 10 '13 at 06:12
  • @bcackerman - in the `after_save` or `after_create` callback, `self` is the record that was just saved, as it exists after the save. That includes autogenerated fields like `id`, `created_at`, `updated_at`. – Chowlett Nov 11 '13 at 09:24
  • Also remember `before_create` refers to a new object being saved to the database, not the actual `create` method being called. Thus, `before_create` can still be fired even from the `save` method. – Steve Dec 21 '14 at 07:04
  • This is a really old question but can someone explain where around_* callbacks fit here ? around_save, around_create ? @Chowlett – Niyanta May 03 '16 at 13:09
  • @Niyanta - looks like you want [this question](http://stackoverflow.com/questions/4998553/rails-around-callbacks). – Chowlett May 03 '16 at 15:09
  • 1
    @Rads - er, no? The docs still list `before_save` 3rd, and `before_create` 5th. – Chowlett Apr 12 '17 at 21:28
  • the answer should include also "on_commit" – Tim Kretschmer Apr 14 '17 at 05:40
  • @Chowlett If I need a build a `has_many` association in `before_*` callback, which one should be used so that validations are fired for the associated model as well? `before_create` or `before_validation`? – mrudult Jun 29 '17 at 18:05
  • @mrudult - I don't know for sure. I'd guess that it doesn't matter - the associated model will be validated anyway. Of course, if the main model validates presence on the association, you'll need `before_validation`. Maybe ask a new question for more details? – Chowlett Jun 30 '17 at 10:28
148

before_save is called every time an object is saved. So for new and existing objects. (create and update action)

before_create only before creation. So only for new objects (create action)

aidan
  • 9,310
  • 8
  • 68
  • 82
Michael Koper
  • 9,586
  • 7
  • 45
  • 59
3

before_create vs before_save :on => :create

Sometimes you have to be careful of the order of the callbacks

See here for more details: http://pivotallabs.com/activerecord-callbacks-autosave-before-this-and-that-etc/

shantanoo
  • 3,617
  • 1
  • 24
  • 37
23inhouse
  • 1,889
  • 19
  • 18