Includes and references association - aavedula/how-to-notes GitHub Wiki

Back

Rails command

  Member.
    includes(:population).
    references(:population).
    where("populations.kind = 2")

SQL query

  SQL (0.6ms)
  SELECT "members"."id" AS t0_r0,
         "members"."population_id" AS t0_r1,
         "members"."pid_type_code" AS t0_r2,
         "members"."pid" AS t0_r3,
         "members"."patient_identity_id" AS t0_r4,
         "members"."patient_identity_effective_at" AS t0_r5,
         "members"."latest_assertion_effective_at" AS t0_r6,
         "members"."latest_assertion_effective_time_zone" AS t0_r7,
         "members"."latest_assertion_effective_on" AS t0_r8,
         "populations"."id" AS t1_r0,
         "populations"."kind" AS t1_r1,
         "populations"."name" AS t1_r2,
         "populations"."time_zone" AS t1_r3,
         "populations"."created_at" AS t1_r4,
         "populations"."updated_at" AS t1_r5,
         "populations"."oid" AS t1_r6,
         "populations"."allscripts_organization_b2b_code" AS t1_r7,
         "populations"."cms_certification_number" AS t1_r8,
         "populations"."region" AS t1_r9,
         "populations"."city" AS t1_r10,
         "populations"."state" AS t1_r11,
         "populations"."postal_code" AS t1_r12,
         "populations"."ccd_custodian_org_name" AS t1_r13
  FROM "members"
  LEFT OUTER JOIN "populations"
  ON "populations"."id" = "members"."population_id"
  WHERE (populations.kind = 2)

Return value

Loads populations, returns Member records.