Rails - ActiveRecord的where.not方法详解(copy)
【说明:资料来自https://robots.thoughtbot.com/activerecords-wherenot】
ActiveRecord's where.not
Rails 4.0 introduced a helpful new method for ActiveRecord queries: where.not
. It can make clunky queries easier to read.
Usage
This query:
User.where.not(name: 'Gabe')
is effectively the same as this:
User.where('name != ?', 'Gabe')
It’s “effectively” the same because where.not
has some extra juice: it will fully qualify the column name with the table name, continue to work if the table or column get aliased (during a left outer join clause with includes), and will continue to work if the database implementation is switched.
I’ve usually seen it used for NOT NULL
queries:
# Old and busted
# User.where('name IS NOT NULL')
# New hotness
User.where.not(name: nil)
But it works with arrays too:
# Without `where.not`
# Something.where("name NOT IN ?", User.unverified.pluck(:name))
# With `where.not`
Something.where.not(name: User.unverified.pluck(:name))
That example takes advantage of the fact that ActiveRecord automatically uses IN
(or in this case NOT IN
) if the value you’re querying against is an array.
Complex usage
Here’s a more complex example:
class Course < ActiveRecord::Base
def self.with_no_enrollments_by(student)
includes(:enrollments).
references(:enrollments).
where.not(enrollments: { student_id: student.id })
end
end
You can ignore the first two lines, which tell ActiveRecord that we’re going through the enrollments
table (student has_many :courses, through: :enrollments
). The method finds courses where the course has no enrollments by the student. It is the complement to student.courses
.
Without where.not
, it would look like this:
def with_no_enrollments_by(student)
includes(:enrollments).
references(:enrollments).
where('enrollments.student_id != ?', student.id)
end
I prefer the pure-Ruby approach of the where.not
version instead of the string SQL of the latter because it’s easier to read and it’s easier to change later.
What’s next
If you found this post helpful, I recommend our post on null relations or a close reading of the official ActiveRecord docs.
Rails - ActiveRecord的where.not方法详解(copy)的更多相关文章
- session的使用方法详解
session的使用方法详解 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台WWW服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台WWW服务器 ...
- Kooboo CMS - Html.FrontHtml[Helper.cs] 各个方法详解
下面罗列了方法详解,每一个方法一篇文章. Kooboo CMS - @Html.FrontHtml().HtmlTitle() 详解 Kooboo CMS - Html.FrontHtml.Posit ...
- HTTP请求方法详解
HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源] GET方法用来请求已被URI识别的资源.指定 ...
- ecshop后台增加|添加商店设置选项和使用方法详解
有时候我们想在Ecshop后台做个设置.radio.checkbox 等等来控制页面的显示,看看Ecshop的设计,用到了shop_config这个商店设置功能 Ecshop后台增加|添加商店设置选项 ...
- (转)Spring JdbcTemplate 方法详解
Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...
- C++调用JAVA方法详解
C++调用JAVA方法详解 博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...
- windows.open()、close()方法详解
windows.open()方法详解: window.open(URL,name,features,replace)用于载入指定的URL到新的或已存在的窗口中,并返回代表新窗口的Win ...
- CURL使用方法详解
php采集神器CURL使用方法详解 作者:佚名 更新时间:2016-10-21 对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...
- JAVA 注解的几大作用及使用方法详解
JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...
随机推荐
- msp430入门编程27
msp430中C语言开发工具调试程序 msp430入门学习 msp430入门编程
- 使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析
使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析 使用搜索引擎计数构建快速.高效和可扩展的数据驱动应用程序 Apache Lucene™ 和 Solr™ 是强大的开源搜索技术, ...
- [Noip复习知识点][个人向]Zackzh
只是列列一些要复习的,努力复习吧,有种noip退役的赶脚. 一.模拟 (这你也不会?退役吧) 二.DP 1.基础dp 2.区间dp 3.状压dp 4.树形dp 6.概率(期望)dp 7.环形dp 8. ...
- linux signal 列表
Linux 信号表 Linux支持POSIX标准信号和实时信号.下面给出Linux Signal的简表,详细细节可以查看man 7 signal. 默认动作的含义如下: Term 终止进程 ...
- Python的环境变量设置
python安装完成后,它的配置很简单,只需要配置下环境变量就可以了. 具体来讲,就是将python的安装目录加入到系统的path中即可.
- datasnap使用ipv6
有些人说DATASNAP不支持IPv6,只支持IPv4. 这是不正确的. DATASNAP默认是使用IPv4在ipv6 环境下 怎样用datasnap?Params.Values['Communica ...
- Meteor核心API
在本教程中,我们将介绍学习Meteor核心API. 如果你想限制代码只在服务器或客户端可以使用下面的代码运行 - meteorApp.js if (Meteor.isClient) { // Code ...
- ubuntu磁盘分区和挂载
- 【原创】PHP扩展开发入门
PHP扩展开发入门 作者:wf (360电商技术组) 在我们编写自己的第一个php扩展之前,先了解一下php的总体架构和执行机制. php的架构如图1所看到的. 当中一个重要的就是SAPI(serve ...
- Angular2.x-显示heroes列表
在此页面中,您将展开Tour of Heroes应用程序以显示heroes列表,并允许用户选择heroes并显示heroes的详细信息. 6.X 你需要一些heroes来展示. 最终你会从远程数据服务 ...