How to query all records that don't have a spe

2019-09-17 11:31发布

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条回答
Juvenile、少年°
2楼-- · 2019-09-17 12:04
sun_open = Shop.includes(:opening_times).where("opening times.day = ?", "Sunday").ids;
sunday_closed = Shop.where("id NOT IN (?)", sunday_open);
查看更多
登录 后发表回答