Rails: around_* callbacks

后端 未结 2 1178
天命终不由人
天命终不由人 2020-11-29 20:24

I have read the documentation at http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html, but don\'t understand when the around_* callbacks are triggered

2条回答
  •  感情败类
    2020-11-29 20:44

    The around_* callback is called around the action and inside the before_* and after_* actions. For example:

    class User
      def before_save
        puts 'before save'
      end
    
      def after_save
        puts 'after_save'
      end
    
      def around_save
        puts 'in around save'
        yield # User saved
        puts 'out around save'
      end
    end
    
    User.save
      before save
      in around save
      out around save
      after_save
    => true
    

提交回复
热议问题