Rails model “before_filter”?

后端 未结 2 1232
栀梦
栀梦 2020-12-31 09:39

I know that before_filter is only for controllers in Rails, but I would like something like this for a model: any time a method in my model is called, I\'d like to run a met

2条回答
  •  孤城傲影
    2020-12-31 10:18

    class MyModel
        extend ActiveModel::Callbacks
        define_model_callbacks :do_stuff
    
        before_do_stuff :confirm
    
        def do_stuff
            run_callbacks :do_stuff do
                #your code
            end
        end
    
        def confirm
            #confirm
        end
    end
    

    I'm really not sure this will work, but you can try it, as I really dont have time now. Take a look at that: http://api.rubyonrails.org/classes/ActiveModel/Callbacks.html

提交回复
热议问题