我有一个链式查询,它生成我需要的确切查询,但我无法访问任何模型对象中的一个键列。 示例链如下:
Organization.select("bt.total_points total_points").
includes(:current_summary).
eager_load(:location).
joins("JOIN #{Benefit.select("SUM(nen_score) as total_points, benefits.organization_id").
group(:organization_id).as("bt").to_sql} ON bt.organization_id= organizations.id")
此处组织与福利模型有一对多关系,一对一与位置,... 请注意,根据用户选择的内容,此链将进一步向下附加条款和订单条件。
我尝试在返回的对象上使用 read_attribute total_points ,但我找不到任何对象中的列。
根据要求生成的查询(为了简洁起见,我删除了很多列)
SELECT bt.total_points as total_points,
"organizations"."id" AS t0_r0,
"organizations"."name" AS t0_r1,
"organizations"."primary_role" AS t0_r5,
"organizations"."founded_on" AS t0_r10,
"organizations"."created_at" AS t0_r11,
"organizations"."updated_at" AS t0_r12,
"organizations"."city" AS t0_r13,
"organizations"."industry_id" AS t0_r15,
"organizations"."location_id" AS t0_r16,
"organizations"."summary_id" AS t0_r17,
"locations"."id" AS t1_r0,
"locations"."name" AS t1_r1,
"locations"."parent_id" AS t1_r2,
"locations"."path" AS t1_r3,
"locations"."created_at" AS t1_r4,
"locations"."updated_at" AS t1_r5,
"organization_performance_snapshots"."id" AS t2_r0,
"organization_performance_snapshots"."organization_id" AS t2_r1,
"organization_performance_snapshots"."valid_from" AS t2_r2,
"organization_performance_snapshots"."valid_till" AS t2_r3,
"organization_performance_snapshots"."period" AS t2_r4,
"organization_performance_snapshots"."revenue" AS t2_r7,
"organization_performance_snapshots"."profit_after_tax" AS t2_r9,
"organization_performance_snapshots"."total_debt" AS t2_r11,
"organization_performance_snapshots"."created_at" AS t2_r14,
"organization_performance_snapshots"."updated_at" AS t2_r15
FROM "organizations"
LEFT OUTER JOIN "locations" ON "locations"."id" = "organizations"."location_id"
LEFT OUTER JOIN "organization_performance_snapshots" ON "organization_performance_snapshots"."organization_id" = "organizations"."id" AND (valid_till IS NULL)
JOIN (SELECT SUM(nen_score) as total_points, benefits.organization_id
FROM "benefits"
GROUP BY "benefits"."organization_id") bt
ON bt.organization_id= organizations.id
ORDER BY "organizations"."id" DESC