Ruby On Rails查询结果不符预期:Station关联BooReports返回全部报告
你遇到的这个问题其实是ActiveRecord中joins方法的典型行为——它执行的是SQL内连接,会把每个匹配条件的BooReport和对应的Station关联起来返回。也就是说,如果一个Station在指定日期范围内有3条BooReports记录,查询结果里就会出现3条该Station的条目,每条对应一个符合条件的报告,这就是为什么你看到每个Station返回了所有关联报告的原因。
接下来根据你的实际需求,给你几种常见场景的解决方案:
场景1:仅获取存在符合条件报告的Station,每个Station只返回一次
如果你只是想确认哪些Station在指定日期范围内有至少一条BooReport,不需要携带报告数据,只需要每个Station出现一次,那给查询加上distinct即可:
@stations = Station.joins(:boo_reports) .where(boo_reports: {date: params[:from].to_date..params[:to].to_date.tomorrow}) .distinct
distinct会让SQL返回去重后的Station记录,避免因为多个匹配的报告导致重复的Station条目。
场景2:每个Station返回一条记录,同时携带符合条件的BooReports数据
如果你需要每个Station只返回一条,同时加载该Station在日期范围内的所有BooReports,那可以结合includes和distinct:
@stations = Station.includes(:boo_reports) .where(boo_reports: {date: params[:from].to_date..params[:to].to_date.tomorrow}) .distinct
这里includes会预加载符合条件的BooReports,同时distinct保证每个Station只出现一次。
场景3:每个Station仅返回一条符合条件的BooReport(比如最新的)
如果你的需求是每个Station只取一条符合日期范围的报告(比如最新生成的那条),可以根据数据库类型选择不同的写法:
针对PostgreSQL
PostgreSQL支持distinct on语法,可以指定按Station去重,同时保留你想要的那条报告:
@stations = Station.joins(:boo_reports) .where(boo_reports: {date: params[:from].to_date..params[:to].to_date.tomorrow}) .order('stations.id, boo_reports.date DESC') # 按报告日期倒序,取最新的 .distinct_on(:station_id)
针对MySQL
MySQL可以用group by结合聚合函数来实现,比如取每个Station最新的报告:
@stations = Station.joins(:boo_reports) .where(boo_reports: {date: params[:from].to_date..params[:to].to_date.tomorrow}) .group('stations.id') .order('MAX(boo_reports.date) DESC')
这样就能保证每个Station只返回一条关联的报告记录了。
内容的提问来源于stack exchange,提问作者hany

