ActiveRecord’s queries tricks 小记

news/2024/6/18 14:01:15

ActiveRecord’s queries tricks 小记

原文 https://medium.com/rubyinside...

关联表join时使用条件

# User model
scope :activated, ->{
  joins(:profile).where(profiles: { activated: true })
}

更好的做法

# Profile model
scope :activated, ->{ where(activated: true) }
# User model
scope :activated, ->{ joins(:profile).merge(Profile.activated) }

关于 merge
https://apidock.com/rails/Act...
https://api.rubyonrails.org/c...

嵌套join的差异

  • User has_one Profile
  • Profile has_many Skills
User.joins(:profiles).merge(Profile.joins(:skills))
=> SELECT users.* FROM users 
   INNER JOIN profiles    ON profiles.user_id  = users.id
   LEFT OUTER JOIN skills ON skills.profile_id = profiles.id
# So you'd rather use:
User.joins(profiles: :skills)
=> SELECT users.* FROM users 
   INNER JOIN profiles ON profiles.user_id  = users.id
   INNER JOIN skills   ON skills.profile_id = profiles.id
   

内链接和外连接

Exist query

存在和不存在

# Post
scope :famous, ->{ where("view_count > ?", 1_000) }
# User
scope :without_famous_post, ->{
  where(_not_exists(Post.where("posts.user_id = users.id").famous))
}
def self._not_exists(scope)
  "NOT #{_exists(scope)}"
end
def self._exists(scope)
  "EXISTS(#{scope.to_sql})"
end

Subqueries 子查询

比如查询部分用户(user)的帖子(post)

不好的做法

Post.where(user_id: User.created_last_month.pluck(:id))

这里的缺陷是将运行两个SQL查询:一个用于获取用户的ID,另一个用于从这些user_id获取帖子

这样写一个查询就可以了

Post.where(user_id: User.created_last_month)

基础

.to_sql 生成 SQL 语句字符串
.explain 获取查询分析

Booleans

对于User.where.not(tall: true)在pg下会生成
SELECT users.* FROM users WHERE users.tall <> 't'
这返回 tall 是 false 的 记录,不包括是null 的

包括null应该这么写

User.where("users.tall IS NOT TRUE")

or

User.where(tall: [false, nil])

http://www.niftyadmin.cn/n/1493290.html

相关文章

机器学习训练营

1 Python基础与性能调优 monad的简单介绍及应用 加餐&#xff1a;深度效率优化及CUDA编程 2 机器学习基本概念 公理化概率体系 3 手撸机器学习算法 4 经典机器学习算法及调优 5 模型集成方法 6 特征工程方法论 7 深度学习基础及常见网络 8 PyTorch基础 9 深度学习模型调优 10 …

神经网络与深度学习-课后习题

《神经网络与深度学习-邱锡鹏》习题解答 https://github.com/nndl/solutions 面试锦囊之LR 面试篇——线性回归怎么问&#xff1f; 面试篇——SVM怎么问 面试篇——决策树/集成学习&#xff08;上篇&#xff09; 面试篇——决策树/集成学习&#xff08;下篇&#xff09…

一种应用程序命令执行架构设计

一种应用程序命令执行架构设计 袁永福 2011-7-5 有感于一些程序中ASPX页面中直接编写功能性代码&#xff0c;难于组织和维护&#xff0c;实现不了程序的高度可配置化&#xff0c;自此提出一种应用程序命令执行架构&#xff0c;其架构图如下 在这个架构中&#xff0c;每一个应用…

牛客输入输出

HJ1 字符串最后一个单词长度 #include <stdio.h> #include <string.h>int main() {char str[1000];int a0,i0;while(scanf("%s",str) ! EOF){}astrlen(str);printf("%d",a); }

WCF异步调用

wcf多线程和异步操作http://blog.csdn.net/lordbaby/article/details/7695807 WCF中的异步调用http://www.cnblogs.com/wayfarer/archive/2007/11/09/954256.html WCF异步调用正确实现方法讲解http://developer.51cto.com/art/201002/185107.htm 如何&#xff1a;以异步方式调用…

GridView 动态 添加新行

这是GridView动态添加行的基本代码&#xff0c;可以进行修改扩充&#xff0c;实现自己想要的动态添加行效果。Code 1 //创建一个GridView的一个分隔行(根据DataControlRowType来设置)2 GridViewRow rowSeparator newGridViewRow(0, 0, DataControlRowType.Separator, DataContr…

类别不平衡问题

详解类别不平衡问题 卢总-类别不平衡问题的方法汇总 文章目录从多数类别中删除数据&#xff08;ENN、Tomeklink、NearMiss&#xff09;ENNNearMiss为少数类生成新样本&#xff08;SMOTE、Borderline-SMOTE、ADASYN&#xff09;集成方法EasyEnsemble算法BalanceCascade算法算法…

ORACLE函数大全(CSDN)

SQL中的单记录函数1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from dual; A A ZERO SPACE--------- --------- --------- --------- 65 97 48 32 2.CHR给出整数,…