加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 在Rails模型中around_create回调的目的是什么?

发布时间:2020-12-16 20:01:35 所属栏目:百科 来源:网络整理
导读:什么时候执行了around_create回调代码,在什么情况下我们应该使用它? 解决方法 还有这个问题,现在已经找到了答案:around_create允许你基本上在一个方法中同时做一个before_create和一个after_create.你必须使用yield来执行保存. class MyModel ActiveRecord
什么时候执行了around_create回调代码,在什么情况下我们应该使用它?

解决方法

还有这个问题,现在已经找到了答案:around_create允许你基本上在一个方法中同时做一个before_create和一个after_create.你必须使用yield来执行保存.
class MyModel < ActiveRecord::Base
  around_create :my_callback_method

  private
    def my_call_back_method
      # do some "before_create" stuff here

      yield  # this makes the save happen

      # do some "after_create" stuff here
    end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读