如何在包含活动记录查询中指定要限制的内容



我有

Shop.includes(:opening_times)
              .where("opening_times.day =?", Time.now.wday)
              .where("opening_times.opens > ?", @now)
              .order("opening_times.opens")
              .references(:opening_times)
              .limit(12)

每当它到达一个有两个营业时间的商店时,它只返回 11 家商店。我想它限制在 12 opening_times而不是 12 家商店。如何指定我要限制 12 家商店,而不是 12 家opening_times?

奇怪的是,这只发生在我按开放时间订购时。如果我拿走.order("opening_times.opens"),它会返回12家商店。

也许您的一家商店有 2 条opening_times记录,而您的 SQL 引擎同时返回它们。

尝试像这样添加不同的: Shop.distinct.includes(:opening_times).where...

最新更新