查找关联中所有为零的内容 [英] find all that are nil in the association

查看:30
本文介绍了查找关联中所有为零的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个帖子和一个用户.
帖子有_许多用户,用户属于_一个帖子.
我需要一个 find 来查找所有没有类似以下用户的帖子:

So I have a Post and a User.
Post has_many users and a user belongs_to a post.
I need a find that will find all the Posts that dont have any users like the following:

Post.first.users
 => [] 

推荐答案

Post.where("id not in (select post_id from users)")

这篇关于查找关联中所有为零的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆