10

As you know, before_save callbacks are executed prior to before_create callbacks.

Therefore, some people have suggested that in would be better to use before_save :method, :on => :create instead of before_create so that the callback method is executed at the right time in relation to other callbacks (such as autosave callbacks). See, for example, this Pivotal Labs blog post, and this StackOverflow answer.

However, as far as I can tell, the :on => :create option does not achieve the desired effect on a before_save callback. In other words, the callback is executed for every save regardless of whether it is a create or not.

The :on => :create option does appear to be valid for before_validation callbacks, though.

Could someone confirm whether the :on => :create is supposed to work for a before_save? Did it work in previous versions of Rails and is now broken, or are the aforementioned links simply mistaken?

Assuming :on => :create is not valid, is the following acceptable, and/or is there a better way?

before_save :callback_method, :if => :new_record?

Thank you.

Community
  • 1
  • 1
Nathan
  • 7,816
  • 8
  • 33
  • 44

1 Answers1

23

You're right, there is no :on option for before_save callback. But, I don't understand, why use before_save instead of before_create. before_create callback will be called right after before_save.

Of course, you can use before_save :callback_method, :if => :new_record?. But I personally don't like this solution - what if I need to add conditions in the :if option?

If one have a dependency between before_save and before_create callbacks, I`d suggest, to combine 2 callbacks. For instance (pseudocode):

class MyModel < ActiveRecord::Base
  before_create :prepare_x
  before_save :do_something_with_x

  def prepare_x
    @x = 10
  end


  # will not work, because `prepare_x` called after `do_something_with_x`
  def do_something_with_x
    @a = 100 / @x
  end
end

# ||
# ||
# \/

class MyModel < ActiveRecord::Base

  before_save :do_something_with_x

  def do_something_with_x
    @x = 10 if new_record?
    @a = 100 / @x
  end
end
cutalion
  • 4,334
  • 1
  • 38
  • 47
  • thanks for your answer, and in particular for confirming that `:on => :create` is not valid. Combining callbacks sounds like it may be a good idea in the general case, but I'm not sure it applies well to the specific case of trying to prepare data for a callback created by an autosave association, does it? – Nathan May 19 '12 at 00:00
  • 1
    Regarding your question of why I want to use before_save instead of before_create: this is explained well in the blog post I referenced. A callback is being created by core Rails code via an autosave association. The callback I want to create needs to be executed before the autosave callback, and a before_create callback would occur too late. – Nathan May 21 '12 at 17:44
  • 4
    It's too bad that `before_save :on => :create` fails silently. It would be nice if it either worked or threw an exception. – Nathan Long Oct 03 '12 at 15:04
  • 1
    When testing I notice that _before_create_ is called when **creating** objects, but _before_save_ is called when **updating** objects. [This](http://apidock.com/rails/ActiveRecord/Callbacks/before_create) mentions that before_create is called "before" Base.save (may seem confusing). [Here](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html) indicates before_create is called "after" before_save There are nice explanations [here](http://stackoverflow.com/questions/6249475/ruby-on-rails-callback-what-is-difference-between-before-save-and-before-crea/6967362#6967362) too. – Hugo Aug 06 '13 at 10:10