有点惭愧,做rails也2年了,竟然不会用counter_cache,刚才google了counter_cache,感觉这个还挺有用的,举个例子,在sina微博中,微博的评论数和收藏数就可以使用到counter_cache,使用了counter_cache,我们就不需要在数据库中查找该博客下的所有评论,然后再计算评论数了,举个具体的例子吧.
class Blog < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :blog, :counter_cache => true
end
注意: blogs中有一个字段是comments_count(整型),当添加和删除comment的时候,会在它所对应的blogs中的comments_count加或减1,这里需要注意的是destroy和destroy_all,可以触发blogs中的comments_count加或减1,而delete和delete_all则不会。
此外如果要指定特定的名字的话呢,就 :counter_cache => :pinlun_counts,这样在blogs中就使用pinlun_counts,而不是comments_count.
class Blog < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :blog, :counter_cache => true
end
注意: blogs中有一个字段是comments_count(整型),当添加和删除comment的时候,会在它所对应的blogs中的comments_count加或减1,这里需要注意的是destroy和destroy_all,可以触发blogs中的comments_count加或减1,而delete和delete_all则不会。
此外如果要指定特定的名字的话呢,就 :counter_cache => :pinlun_counts,这样在blogs中就使用pinlun_counts,而不是comments_count.
发表评论
-
rails console 记录命令历史
2020-03-20 10:53 2771: 打开文件: vim ~/.irbrc,添加如下内容: ... -
mysql dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
2019-12-16 16:43 357今天一连串的神操作导致连接mysql一直报错,dyld: Li ... -
ruby gsub和sub 的特殊使用
2019-04-04 19:53 954gsub和sub使用 常用的使 ... -
rails离线环境搭建相关命令
2018-12-17 09:35 3851:下载yum包: 环境搭建需要安装很多包,可以使用 yumd ... -
rails离线环境搭建相关命令
2018-12-17 09:35 3991:下载yum包: 环境搭建需要安装很多包,可以使用 yumd ... -
rails5 belongs_to 默认不能为空,报错must exist
2017-04-29 16:37 4451: 在config/application.rb设置 con ... -
随笔知识
2014-08-07 21:11 1786show-source: 查看方法在文件中的位置,show-s ... -
rails 中 group 按照一定的规则返回数据
2012-11-03 19:20 937has_many :user_resumes, class_n ... -
项目 经验
2012-10-10 15:53 01: user = User.first; user. ... -
数组使用will_paginate
2012-03-30 11:58 1409有的时候,在开发的过程中需要对模型数组进行分页,此时我们可以如 ... -
我的rails 编程规范
2012-01-16 15:19 8471: 属性中时间要已at结尾,例如created_at, ... -
html_safe
2012-01-12 13:56 1410html_safe: 可以保证(> < &am ... -
同时分页查找没有关联关系的多个表
2011-12-30 14:41 1248supplyinfos和seekinginfos两个表 需求: ... -
send_file ie下文件名乱码问题解决
2011-10-28 17:50 2427使用rails的send_file 实现下载功能时,type指 ... -
has_many 关联关系表单 提交
2011-04-18 17:16 1118class Author has_many :books ... -
belongs_to 多级关联关系
2011-04-12 14:18 1093有一种情况,是这样的c属于b,b属于a,但是我们有时候想建立c ... -
rails 根据异常显示特定的错误页面
2011-03-27 20:50 1153rescue_from 异常, :with => :a ... -
rails 笔记
2011-03-27 20:49 1699启动项目: ruby script/se ... -
rails 发送电子邮件(ActionMailer)
2011-03-27 20:49 36001: 复制以下配置邮件 ... -
rails 读写分离
2011-03-27 20:48 1977开发环境下实现rails的 ...
相关推荐
gem 'custom_counter_cache' 示例 带计数器缓存的类 这是将用于计算计数器缓存的值的块。 其他模型将通过after_save或after_destroy回调通过它们的关联来调用它。 include CustomCounterCache :: Model define_...
反文化 Turbo为您的Rails应用程序提供计数器缓存。 对Rails标准计数器缓存的巨大改进...product.categories_count # => will use counter cache without query 安装 将counter_culture添加到您的Gemfile中: gem 'co
Laravel开发-lara-counter-cache Laravel的计数器缓存
Laravel开发-counter-cache Laravel的计数器缓存
RDOC 安装Mongoid Magic Counter Cache至少需要ruby 1.9.3Ruby$ [sudo] gem install mongoid_magic_counter_cache宝石文件gem 'mongoid_magic_counter_cache'用法首先,将一个字段添加到文档中,您将从中访问计数器...
Laravel开发-lara-counter-cache .zip.zip
Laravel开发-laravel-delayed-counter-cache 延迟的计数器缓存在事务中增加/减少
$cache_modified_check [缓存修改检查变量] $config_overwrite [配置覆盖变量] $config_booleanize [配置布尔化变量] $config_read_hidden [配置读取隐藏变量] $config_fix_newlines [配置固定换行符变量] $...
$cache_modified_check [缓存修改检查变量] $config_overwrite [配置覆盖变量] $config_booleanize [配置布尔化变量] $config_read_hidden [配置读取隐藏变量] $config_fix_newlines [配置固定换行符变量] $...
发布意见计数器 帖子查看计数器可让您以简单,快速和可靠的方式显示查看过帖子,页面或自定义帖子类型的次数。 描述 可让您以简单,快速和可靠的方式显示过帖子,页面或自定义...兼容W3缓存/ WP SuperCache 可选的对象
System working set paging activity can be viewed by monitoring the Memory: Cache Faults/sec counter. For more information, see also… Inside Windows 2000,Third Edition, p. 463. System Cache The ...
Cache<String> cache = CacheBuilder .newBuilder("redisCacheKeyPrefix") //redis中缓存值的key前缀 .expireTime(100) //过期时间 单位(秒) 默认过期时间为一年 .build(new CacheLoader() { @Override public...
application.setAttribute("counter", Integer.toString(icount)); } %> 您是第("counter") %>位访问者! </body> </html> 简易聊天室本次实验的目的是通过以下题目掌握JSP内置对象,包括...
快取 缓存是一个处理缓存后端的小型库,该库还处理计数器。使用Composer安装composer require sonata-project/cache缓存使用率<...phpuse Sonata \ Cache \ Adapter \ Counter \ PRedisCounter
高并发编程第三阶段12讲 sun.misc.Unsafe介绍以及几种Counter方案性能对比.mp4 高并发编程第三阶段13讲 一个JNI程序的编写,通过Java去调用C,C++程序.mp4 高并发编程第三阶段14讲 Unsafe中的方法使用,一半是...
高并发编程第三阶段12讲 sun.misc.Unsafe介绍以及几种Counter方案性能对比.mp4 高并发编程第三阶段13讲 一个JNI程序的编写,通过Java去调用C,C++程序.mp4 高并发编程第三阶段14讲 Unsafe中的方法使用,一半是...
(based on counter term) * Fix bug for allow Suggested Tags for page CPT. * Version 2.1 : * Add compatibility with WP 3.2 * Fix bug with autocompletion. (jQuery want now a strict content type text/...
KGB Archiver console version ?...based on PAQ6 by Matt Mahoney PAQ6v2 - File archiver and compressor. (C) 2004, Matt Mahoney, mmahoney@cs.fit.edu This program is free software; you can redistribute it ...
Contents Overview 1 Lesson 1: Concepts – Locks and Lock Manager 3 Lesson 2: Concepts – Batch and Transaction 31 Lesson 3: Concepts – Locks and Applications 51 Lesson 4: Information Collection and ...
EurekaLog 7.5 (18-August-2016) 1)..Important: Installation layout was changed. All packages now have version suffix (e.g. EurekaLogCore240.bpl). No files are copied to \bin folder of IDE....