ruby-on-rails – 为什么在使用触摸时不会触发after_save?
发布时间:2020-12-17 04:36:19 所属栏目:百科 来源:网络整理
导读:最近几天,我试图使用Redis商店缓存rails app. 我有两个型号: class Category ActiveRecord::Base has_many :products after_save :clear_redis_cache private def clear_redis_cache puts "heelllooooo" $redis.del 'products' endend 和 class Product Act
最近几天,我试图使用Redis商店缓存rails app.
我有两个型号: class Category < ActiveRecord::Base has_many :products after_save :clear_redis_cache private def clear_redis_cache puts "heelllooooo" $redis.del 'products' end end 和 class Product < ActiveRecord::Base belongs_to :category,touch: true end 在控制器中 def index @products = $redis.get('products') if @products.nil? @products = Product.joins(:category).pluck("products.id","products.name","categories.name") $redis.set('products',@products) $redis.expire('products',3.hour.to_i) end @products = JSON.load(@products) if @products.is_a?(String) end 使用此代码,缓存工作正常. 解决方法
您是否阅读过
touch 方法的文档?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |