ruby-on-rails – 为什么在使用触摸时不会触发after_save?

最近几天,我试图使用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

使用此代码,缓存工作正常.
但是当我更新或创建新产品(我在关系中使用了触摸方法)时,它不会触发Category模型中的after_save回调.
你能解释一下为什么吗?

解决方法

您是否阅读过 touch方法的文档?

Saves the record with the updated_at/on attributes set to the current
time. Please note that no validation is performed and only the after_touch,after_commit and after_rollback callbacks are executed. If an attribute name is passed,that attribute is updated along with updated_at/on attributes.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...