How to call expire_fragment from Rails Observer/Model?

前端 未结 8 874
长发绾君心
长发绾君心 2020-12-23 10:15

I\'ve pretty much tried everything, but it seems impossible to use expire_fragment from models? I know you\'re not supposed to and it\'s non-MVC, but surely there much be

8条回答
  •  清歌不尽
    2020-12-23 10:40

    This might not work for what you're doing, but you may be able to define a custom call back on your model:

    class SomeModel < ActiveRecord::Base
        define_callback :after_exploded
    
        def explode
            ... do something that invalidates your cache ...
            callback :after_exploded
        end
    end
    

    You can then use a sweeper like you would normally:

    class SomeModelSweeper < ActionController::Caching::Sweeper
      observe SomeModel 
    
        def after_exploded(model)
          ... expire your cache
        end
    end
    

    Let me know if this is useful!

提交回复
热议问题