Skip to content

Instantly share code, notes, and snippets.

@Chryus
Last active August 29, 2015 13:56
Show Gist options
  • Save Chryus/8900472 to your computer and use it in GitHub Desktop.
Save Chryus/8900472 to your computer and use it in GitHub Desktop.
Exploring has_many :through relationship in rails console
2.0.0-p247 :053 > carrot = Carrot.first
Carrot Load (0.1ms) SELECT "carrots".* FROM "carrots" ORDER BY "carrots"."id" ASC LIMIT 1
=> #<Carrot id: 9, color: "green", created_at: "2014-02-09 15:02:04", updated_at: "2014-02-09 15:02:04">
2.0.0-p247 :054 > carrot.bunny_ids
(0.1ms) SELECT "bunnies".id FROM "bunnies" INNER JOIN "communities" ON "bunnies"."id" = "communities"."bunny_id" WHERE "communities"."carrot_id" = ? [["carrot_id", 9]]
=> [9]
2.0.0-p247 :058 > carrot.bunnies.collect{|bunny| bunny.name}
=> ["Jasper"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment