Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Created January 3, 2015 22:53
Show Gist options
  • Save luckyruby/68cd5b6947d62246e0c7 to your computer and use it in GitHub Desktop.
Save luckyruby/68cd5b6947d62246e0c7 to your computer and use it in GitHub Desktop.
2.2.0 :045 > p = Player.joins(:team).select("players.name, upper(teams.abbreviation) as team_name").first
Player Load (0.4ms) SELECT players.name, upper(teams.abbreviation) as team_name FROM "players" INNER JOIN "teams" ON "teams"."id" = "players"."team_id" ORDER BY "players"."id" ASC LIMIT 1
=> #<Player id: nil, name: "Kevin Garnett">
2.2.0 :046 > p.attributes
=> {"name"=>"Kevin Garnett", "team_name"=>"BRO", "id"=>nil}
2.2.0 :047 > p = Player.eager_load(:team).select("players.name, upper(teams.abbreviation) as team_name").first
SQL (0.6ms) SELECT players.name, upper(teams.abbreviation) as team_name, "players"."id" AS t0_r0, "players"."first_name" AS t0_r1, "players"."last_name" AS t0_r2, "players"."active" AS t0_r3, "players"."created_at" AS t0_r4, "players"."updated_at" AS t0_r5, "players"."name" AS t0_r6, "players"."team_id" AS t0_r7, "teams"."id" AS t1_r0, "teams"."name" AS t1_r1, "teams"."abbreviation" AS t1_r2, "teams"."short_code" AS t1_r3, "teams"."conference" AS t1_r4, "teams"."division" AS t1_r5, "teams"."created_at" AS t1_r6, "teams"."updated_at" AS t1_r7 FROM "players" LEFT OUTER JOIN "teams" ON "teams"."id" = "players"."team_id" ORDER BY "players"."id" ASC LIMIT 1
=> #<Player id: 3007, first_name: "Kevin", last_name: "Garnett", active: true, created_at: "2014-12-22 07:16:24", updated_at: "2014-12-22 07:16:24", name: "Kevin Garnett", team_id: 2>
2.2.0 :048 > p.attributes
=> {"id"=>3007, "first_name"=>"Kevin", "last_name"=>"Garnett", "active"=>true, "created_at"=>Mon, 22 Dec 2014 07:16:24 UTC +00:00, "updated_at"=>Mon, 22 Dec 2014 07:16:24 UTC +00:00, "name"=>"Kevin Garnett", "team_id"=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment