如何查询没有特定关联的所有记录?

时间:2013-08-03 03:30:13

标签: ruby-on-rails activerecord ruby-on-rails-4

如果我想要一份周日开放的所有商店的清单,我会

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

有没有办法获得周日关闭的所有商店的清单?也就是说,所有与日期栏都是“星期天”的记录无关的商店?

1 个答案:

答案 0 :(得分:2)

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