Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is :on => :create valid for a before_save callback in Rails 3.2.3

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.

like image 824
Nathan Avatar asked May 17 '12 17:05

Nathan


People also ask

How does Callback work in Rails?

Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.

What is around callback in rails?

In Rails, callbacks are hooks provided by Active Record that allow methods to run before or after a create, update, or destroy action occurs to an object. Since it can be hard to remember all of them and what they do, here is a quick reference for all current Rails 5 Active Record callbacks.

Can we save an object in the DB if its validation do not pass?

Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the INSERT or UPDATE operation. This helps to avoid storing an invalid object in the database.

What is Active Record in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.


1 Answers

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
like image 153
cutalion Avatar answered Oct 19 '22 15:10

cutalion