访问 ruby 计数器缓存
我正在尝试使用acts_as_taggable_on_steroids 的分支作为学习练习。我正在查看的版本做了一些我不明白的事情来计算标签计数。所以我想我应该使用 PORC(Plain Old Rails Counters)做一个版本:
class Tagging < ActiveRecord::Base #:nodoc:
belongs_to :tag, :counter_cache => "tagging_counter_cache"
...
我认为当我访问 tag.taggings.count 时, tagging_counter_cache 被透明地访问,但显然不是?我真的必须显式访问 tag.tagging_counter_cache 吗?
>> tag.taggings.count
SQL (0.7ms) SELECT count(*) AS count_all FROM `taggings` WHERE (`taggings`.tag_id = 16)
尺寸相同。
如果是这样的话那就很酷了,但只是想检查一下。
I'm playing around with a fork of acts_as_taggable_on_steroids as a learning exercise. The version I'm looking at does some stuff I don't understand to calculate Tag counts. So I thought I'd do a version using PORC (Plain Old Rails Counters):
class Tagging < ActiveRecord::Base #:nodoc:
belongs_to :tag, :counter_cache => "tagging_counter_cache"
...
I thought tagging_counter_cache was transparently accessed when I access tag.taggings.count but apparently not? Do I really have to access tag.tagging_counter_cache explicitly?
>> tag.taggings.count
SQL (0.7ms) SELECT count(*) AS count_all FROM `taggings` WHERE (`taggings`.tag_id = 16)
Same for size.
It's cool if that's the case but just wanted to check.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对集合调用#size
将返回计数器缓存中的值。调用 #count
将始终强制 sql 调用来获取最新计数。
Calling #size on the collection
will return the value in the counter cache. Calling #count
will always force a sql call to get the latest count.
您是否在迁移中创建了关联列?它需要某个地方来存储缓存。
Did you create the associated column in your migration? It needs someplace to store the cache.