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

栏目: Ruby · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/28122381/why-does-after-save-not-trigger-when-using-touch

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

If an attribute name is passed, that attribute is updated along with

updated_at/on attributes.

翻译自:https://stackoverflow.com/questions/28122381/why-does-after-save-not-trigger-when-using-touch


以上所述就是小编给大家介绍的《ruby-on-rails – 为什么在使用触摸时不会触发after_save?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

阿里传

阿里传

波特·埃里斯曼 / 张光磊、吕靖纬、崔玉开 / 中信出版社 / 2015-9-15 / CNY 49.00

你只知道阿里巴巴故事的中国部分,而这本书会完整呈现故事的全部。 波特•埃里斯曼是阿里巴巴创业时期为数不多的外国高管。他于2000~2008年在阿里巴巴担任副总裁,这本书记录了他在阿里巴巴8年的时间里的创业故事、商业经验以及在阿里巴巴和马云、蔡崇信、关明生等阿里巴巴早期团队并肩奋战的故事。 在波特眼中,阿里巴巴的成功经验和模式是可以复制的,阿里巴巴曾经犯过的错误,走过的弯路,我们也可以绕......一起来看看 《阿里传》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试