如何不具有特定关联的所有记录查询? [英] How to query all records that don't have a specific association?

查看:138
本文介绍了如何不具有特定关联的所有记录查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想所有的店铺都开在周日的清单,我

  Shop.includes(:opening_times)。凡(?opening_times.day =,星期天)
 

有没有办法让所有的都在周日关闭的商店名单?也就是说,所有未用一记,其中一天栏是星期天?相关的商店

解决方案

  sun_open = Shop.includes(:opening_times)。凡(?打开times.day =,星期天 ).ids;
sunday_closed = Shop.where(ID NOT IN()?,sunday_open);
 

If I want a list of all the shops that are open on Sunday, I do

Shop.includes(:opening_times).where("opening_times.day =?", 'Sunday')

Is there any way to get a list of all the shops that are closed on Sundays? That is, all the shops that are not associated with a record where the day column is 'Sunday'?

解决方案

sun_open = Shop.includes(:opening_times).where("opening times.day = ?", "Sunday").ids;
sunday_closed = Shop.where("id NOT IN (?)", sunday_open);

这篇关于如何不具有特定关联的所有记录查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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