How to call expire_fragment from Rails Observer/Model?

前端 未结 8 850
长发绾君心
长发绾君心 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:44

    The solution provided by Orion works perfectly. As an enhancement and for convenience, I've put the following code into config/initializers/active_record_expire_fragment.rb

    class ActiveRecord::Base
      def expire_fragment(*args)
        ActionController::Base.new.expire_fragment(*args)
      end
    end
    

    Now, you can use expire_fragment on all instances of ActiveRecord::Base, e.g. User.first.expire_fragment('user-stats')

提交回复
热议问题