问题
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