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 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...
随机推荐
- [NOIP2003] 提高组 洛谷P1039 侦探推理
题目描述 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏的内容是这样的,明明的同学们先商量好由其中的一个人充当罪犯(在明明不知情的情况下),明 ...
- Codeforces Round #296 (Div. 2) D. Clique Problem [ 贪心 ]
传送门 D. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- solr请求处理器列表
List of Request Handlers Available The Javadocs contain a complete list of Request Handlers. Many of ...
- Java的发送邮件
以下内容引用自http://wiki.jikexueyuan.com/project/java/sending-email.html: 用Java应用程序来发送一封电子邮件是足够简单的,但是开始时应该 ...
- Eclipse通过Maven构建时出现: Fatal error compiling: tools.jar not found: Fatal error compiling: tools.jar not found: C:\Program Files\Java\jre1.8.0_31\..\lib\tools.jar
错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-com ...
- vue-alioss-组件封装
<template> <div class="vui_alioss_upload"> <div @click="uloadImg()&quo ...
- FTRL (Follow-the-regularized-Leader)算法
Online gradient descent(OGD) produces excellent prediction accuracy with a minimum of computing reso ...
- 【python】super()
转自: http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035005.html
- Android - 渠道号(vender)
渠道号(vender) 本文地址: http://blog.csdn.net/caroline_wendy Android的apk公布,须要统计各个渠道(vendor)的激活数.就能够使用vendor ...
- iOS iOS8中 问题"registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later" 解决方式
问题重述: iOS 8中改变了通知注冊的方式,假设App须要同一时候支持iOS 7 和 8 的话,须要首先检查selector. 解决方式:在Xcode 6中 - (BOOL)application: ...