开发者

how to skip before_save filter in Rails for specific command

开发者 https://www.devze.com 2022-12-25 13:27 出处:网络
I have a before_save defined as follows: def before_save self.token = generate_token end I want to skip it for specific save method calls. so in my code I would like to do

I have a before_save defined as follows:

  def before_save
    self.token = generate_token
  end

I want to skip it for specific save method calls. so in my code I would like to do

开发者_JAVA技巧@user.save

without before_save filter getting called. Can I do that?


You can do:

 @user.send(:update_without_callbacks)

Or create_without_callbacks works as well. I have used both but I don't know if there is a "save_without_callbacks" Either way, use sparingly.


You could also include conditional logic in your method to handle the cases where you wouldn't want the callback to fire.

def before_save
  self.token = generate_token if token.blank?
end

Not a great example, but you get the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消