Rails cache_digests touch has_many关联 [英] Rails cache_digests touch has_many association
问题描述
我有这样的东西:
class Suite < ActiveRecord::Base
has_many :tests
end
class Test < ActiveRecord::Base
belongs_to :suite
end
我正在使用cache_digests gem执行片段缓存。
我希望在更新Suite对象时,子级测试缓存过期。
我尝试将 touch:true
放入 has_many
关联中,但未成功。
And I'm using the cache_digests gem to do fragment caching.
I want that when I update a Suite object, the children tests caches expire.
I tried to put a touch: true
in the has_many
association without success.
我该怎么做?
预先感谢
我正在像这样进行缓存:
I was doing my cache like this:
<% cache test do %>
<tr>
etc...
<% cache test.suite do %>
etc..
<% end %>
</tr>
<% end %>
但这是行不通的,因为当我编辑套件时,他们的测试没有被触及。因此,我将缓存声明更改为以下内容:
But it doesn't work, because when I edit a suite, their tests isn't touched. So, I changed my cache declaration to something like this:
<% cache [test, test.suite] do %>
etc..
<% end %>
它按预期方式工作。
当我编辑测试或套件时,其中的一个被触摸了,因此,该片段过期了,并且我按预期获得了新版本。
When I edit a test, or a suite, one of those is touched, so, the fragment got expired and I got the new version just as expected.
感谢
推荐答案
此页面:
https://github.com/rails/rails/issues/8759
建议使用after_save钩子:
Suggest using an after_save hook:
class Post < ActiveRecord::Base
has_many :assets
after_save -> { self.touch }
end
class Asset < ActiveRecord::Base
belongs_to :post
end
这篇关于Rails cache_digests touch has_many关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!