Ruby on Rails Callback, what is difference between :before_save and :before_create?
Asked Answered
T

3

199

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?

Tilda answered 6/6, 2011 at 8:37 Comment(0)
O
389

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.

Outset answered 6/6, 2011 at 8:46 Comment(10)
before_validation_on_create and after_validation_on_create are removed as of Rails 3, instead use before_validation and after_validation respectively with option :on => :create.Spark
How would you refer to the record that was just created when using after_save or after_create?Dannielledannon
@Dannielledannon - 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.Outset
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.Basinger
This is a really old question but can someone explain where around_* callbacks fit here ? around_save, around_create ? @OutsetInosculate
@Inosculate - looks like you want this question.Outset
@Rads - er, no? The docs still list before_save 3rd, and before_create 5th.Outset
the answer should include also "on_commit"Egger
@Outset 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?Arrowwood
@Arrowwood - 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?Outset
N
150

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)

Nonaggression answered 6/6, 2011 at 8:47 Comment(0)
R
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/

Regeniaregensburg answered 6/8, 2011 at 14:10 Comment(2)
before_save :on => :create doesn't work (at least on rails 3.2)Mice
You can use: before_save :generate_api_key, :if => :new_record?Marnimarnia

© 2022 - 2024 — McMap. All rights reserved.