rails 下 has_many时 count size length的不同
发布日期:2021-09-29 20:09:17
浏览次数:33
分类:技术文章
本文共 1385 字,大约阅读时间需要 4 分钟。
所以,count会直接算 [quote] In Ruby, #length and #size are synonyms and both do the same thing: they tell you how many elements are in an array or hash. Technically #length is the method and #size is an alias to it. In ActiveRecord, there are several ways to find out how many records are in an association, and there are some subtle differences in how they work. post.comments.count - Determine the number of elements with an SQL COUNT query. You can also specify conditions to count only a subset of the associated elements (e.g. :conditions => {:author_name => "josh"}). If you set up a counter cache on the association, #count will return that cached value instead of executing a new query. post.comments.length - This always loads the contents of the association into memory, then returns the number of elements loaded. Note that this won't force an update if the association had been previously loaded and then new comments were created through another way (e.g. Comment.create(...) instead of post.comments.create(...)). post.comments.size - This works as a combination of the two previous options. If the collection has already been loaded, it will return its length just like calling #length. If it hasn't been loaded yet, it's like calling #count. That's I always have to look up these differences, so now I have them in one place so I don't have to think about it anymore. [/quote]转载地址:https://blog.csdn.net/horace_lee/article/details/84081642 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月07日 21时41分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
后缀树建立 java_实用算法实现-第 8 篇后缀树和后缀数组 [2 最长公共子串]
2019-04-21
java网络编程期末试题_java网络编程面试题【其中一小部分】
2019-04-21
java如何做表单校验_微信小程序实现表单校验功能
2019-04-21
matlab dwt2(),MATLAB小波变换指令及其功能介绍(超级有用)
2019-04-21
php sequelize,egg.js整合数据库ORM框架Sequelize
2019-04-21
php同时打开2个数据库,thinkphp3.2同时连接两个数据库的简单方法
2019-04-21
centos 开发php扩展,centos 安装php扩展redis
2019-04-21
cms 单点登录 php,Yii2 中实现单点登录的方法
2019-04-21
oracle自己运行,创建Oracle自动执行Job
2019-04-21
chmod 赋权所有_chmod 权限 命令详细用法
2019-04-21
html代码翻译_[译]您知道 HTML 的键盘标签吗?
2019-04-21
html抽奖代码_JavaScript高手之路:封装抽奖效果
2019-04-21
的流程图做完后如何保存_2019超火的半永久眉是哪款?做完后我们如何护理?...
2019-04-21
去除logo 高德地图api_深圳品牌logo升级如何保持原型的同时更具创新?
2019-04-21
二重积分转换成极坐标_二重积分转换极坐标r的范围如何确定?
2019-04-21