What is purpose of around_create callback in Rails Model?

前端 未结 6 2047
野的像风
野的像风 2021-02-05 15:42

when is around_create callback code executed, in what situations we should use it?

6条回答
  •  自闭症患者
    2021-02-05 15:49

    Had this question, too, and have now found the answer: around_create allows you to basically do both a before_create and an after_create in one method. You have to use yield to execute the save in between.

    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
    

提交回复
热议问题