How to query all records that don't have a specific association?

半城伤御伤魂 提交于 2019-12-24 21:43:39

问题


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'?


回答1:


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


来源:https://stackoverflow.com/questions/18028988/how-to-query-all-records-that-dont-have-a-specific-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!