Rails eager_load与关联条件

时间:2016-10-30 13:48:39

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

我有一个Rails应用程序,它有 Stations (气象站)和 Observations 。该应用程序在地图上显示当前风速和风向的许多气象站。

我有一个方法用于stations#index方法,该方法选择站点并加入每个站点的最新观测值。

class Station < ActiveRecord::Base
  has_many :observations
  def self.with_observations(limit = 1)
    eager_load(:observations).where(
      observations: { id: Observation.pluck_from_each_station(limit) }
    )
  end
end

Observation.pluck_from_each_station返回一组id。 observations表包含数千行,因此必须保持rails不要急于加载数千条记录。

此方法应返回所有 - 是否有观察。但目前情况并非如此。

it "includes stations that have no observations" do
  new_station = create(:station)
  stations = Station.with_observations(2)
  expect(stations).to include new_station # fails
end

根据我的理解,LEFT OUTER JOIN应返回连接表中是否有任何结果的所有行。为什么这不按预期工作?

这是SQL生成的一个示例:

SELECT "stations"."id" AS t0_r0,
       "stations"."name" AS t0_r1,
       "stations"."hw_id" AS t0_r2,
       "stations"."latitude" AS t0_r3,
       "stations"."longitude" AS t0_r4,
       "stations"."balance" AS t0_r5,
       "stations"."timezone" AS t0_r6,
       "stations"."user_id" AS t0_r7,
       "stations"."created_at" AS t0_r8,
       "stations"."updated_at" AS t0_r9,
       "stations"."slug" AS t0_r10,
       "stations"."speed_calibration" AS t0_r11,
       "stations"."firmware_version" AS t0_r12,
       "stations"."gsm_software" AS t0_r13,
       "stations"."description" AS t0_r14,
       "stations"."sampling_rate" AS t0_r15,
       "stations"."status" AS t0_r16,
       "observations"."id" AS t1_r0,
       "observations"."station_id" AS t1_r1,
       "observations"."speed" AS t1_r2,
       "observations"."direction" AS t1_r3,
       "observations"."max_wind_speed" AS t1_r4,
       "observations"."min_wind_speed" AS t1_r5,
       "observations"."temperature" AS t1_r6,
       "observations"."created_at" AS t1_r7,
       "observations"."updated_at" AS t1_r8,
       "observations"."speed_calibration" AS t1_r9
FROM   "stations"
       LEFT OUTER JOIN
       "observations"
       ON "observations"."station_id" = "stations"."id"
WHERE  "observations"."id" IN (450, 500, 550, 600, 650, 700, 750, 800);

1 个答案:

答案 0 :(得分:1)

我认为会发生这种情况,因为你排除"observations"."id"之后left join为空的记录:

eager_load(:observations).where(
  '"observations"."id" is null or "observations"."id" in (?)', Observation.pluck_from_each_station(limit)
)

在两个条件下逻辑left join相同,但由于rails没有此功能,您可以使用where子句解决它。