是:on =>:对Rails 3.2.3中的before_save回调创建有效

时间:2021-08-15 23:28:19

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

如您所知,before_save回调是在before_create回调之前执行的。

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 * answer.

因此,有些人建议最好使用before_save:method,:on =>:create而不是before_create,以便在适当的时间执行与其他回调相关的回调方法(例如自动保存回调)。例如,请参阅此Pivotal Labs博客文章以及此*答案。

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.

但是,据我所知,:on =>:create选项无法实现对before_save回调的预期效果。换句话说,无论是否是创建,都会为每次保存执行回调。

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

但是:on =>:create选项似乎对before_validation回调有效。

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?

有人可以确认:on =>:create是否应该适用于before_save?它是否在以前的Rails版本中有效并且现在已经坏了,或者前面提到的链接是错误的?

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

假设:on =>:create无效,以下是否可接受,和/或有更好的方法吗?

before_save :callback_method, :if => :new_record?

Thank you.

1 个解决方案

#1


16  

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.

你是对的,对于before_save回调没有:on选项。但是,我不明白,为什么使用before_save而不是before_create。 before_create回调将在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?

当然,你可以使用before_save:callback_method,:if =>:new_record?。但我个人不喜欢这个解决方案 - 如果我需要在:if选项中添加条件怎么办?

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

如果有一个在before_save和before_create回调之间存在依赖关系,我建议组合2个回调。例如(伪代码):

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

#1


16  

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.

你是对的,对于before_save回调没有:on选项。但是,我不明白,为什么使用before_save而不是before_create。 before_create回调将在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?

当然,你可以使用before_save:callback_method,:if =>:new_record?。但我个人不喜欢这个解决方案 - 如果我需要在:if选项中添加条件怎么办?

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

如果有一个在before_save和before_create回调之间存在依赖关系,我建议组合2个回调。例如(伪代码):

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