Ruby on rails 如何在includes activerecord查询中指定要限制的内容?

Ruby on rails 如何在includes activerecord查询中指定要限制的内容?,ruby-on-rails,ruby,activerecord,ruby-on-rails-4,rails-postgresql,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 4,Rails Postgresql,我有 每当它击中一家有两次营业时间的商店时,它只返回11家商店。我想它限制了12家店铺的开业时间,而不是12家店铺。我如何指定我要限制12家店铺,而不是12个营业时间 奇怪的是,这种情况只有在我按开盘时间点菜时才会发生。如果我拿走.order(“opening\u times.opens”),它会返回12家店铺。也许您的一家店铺有2条opening\u times记录,而您的SQL引擎会同时返回这两条记录 尝试像这样添加distinct:Shop.distinct.includes(:开业时间)

我有

每当它击中一家有两次营业时间的商店时,它只返回11家商店。我想它限制了12家店铺的开业时间,而不是12家店铺。我如何指定我要限制12家店铺,而不是12个营业时间


奇怪的是,这种情况只有在我按开盘时间点菜时才会发生。如果我拿走
.order(“opening\u times.opens”)
,它会返回12家店铺。

也许您的一家店铺有2条opening\u times记录,而您的SQL引擎会同时返回这两条记录


尝试像这样添加distinct:
Shop.distinct.includes(:开业时间)。其中…

也许您的一家店铺有两条开业时间记录,并且您的SQL引擎会同时返回这两条记录


尝试这样添加distinct:
Shop.distinct.includes(:opening_times)。where…

我不明白为什么,但当我这样做时,它会显示
PG::SyntaxError:ERROR:syntax ERROR位于或靠近“distinct”行1的语法错误:选择distinct distinct
。不知何故,它显示出明显的两次。我很困惑。我不明白为什么,但当我这么做的时候,它会说
PG::SyntaxError:ERROR:syntaxer ERROR位于或接近“DISTINCT”行1:选择DISTINCT
。不知何故,它显示出明显的两次。我很困惑。
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)