Last active
August 29, 2015 13:56
-
-
Save Chryus/8900461 to your computer and use it in GitHub Desktop.
Exploring has_many :through relationship in the console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2.0.0-p247 :042 > bunny = Bunny.first | |
Bunny Load (0.2ms) SELECT "bunnies".* FROM "bunnies" ORDER BY "bunnies"."id" ASC LIMIT 1 | |
=> #<Bunny id: 9, name: "Jasper", created_at: "2014-02-09 15:01:25", updated_at: "2014-02-09 15:01:25"> | |
2.0.0-p247 :043 > bunny.carrot_ids | |
(0.1ms) SELECT "carrots".id FROM "carrots" INNER JOIN "communities" ON "carrots"."id" = "communities"."carrot_id" WHERE "communities"."bunny_id" = ? [["bunny_id", 9]] | |
=> [] | |
2.0.0-p247 :044 > bunny.carrot_ids = [9, 10] | |
Carrot Load (0.3ms) SELECT "carrots".* FROM "carrots" WHERE "carrots"."id" IN (9, 10) | |
Carrot Load (0.1ms) SELECT "carrots".* FROM "carrots" INNER JOIN "communities" ON "carrots"."id" = "communities"."carrot_id" WHERE "communities"."bunny_id" = ? [["bunny_id", 9]] | |
(0.1ms) begin transaction | |
SQL (0.4ms) INSERT INTO "communities" ("bunny_id", "carrot_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["bunny_id", 9], ["carrot_id", 9], ["created_at", Sun, 09 Feb 2014 15:09:39 UTC +00:00], ["updated_at", Sun, 09 Feb 2014 15:09:39 UTC +00:00]] | |
SQL (0.1ms) INSERT INTO "communities" ("bunny_id", "carrot_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["bunny_id", 9], ["carrot_id", 10], ["created_at", Sun, 09 Feb 2014 15:09:39 UTC +00:00], ["updated_at", Sun, 09 Feb 2014 15:09:39 UTC +00:00]] | |
(8.0ms) commit transaction | |
=> [9, 10] | |
2.0.0-p247 :080 > bunny.carrots.collect{|carrot| carrot.color} | |
=> ["green", "orange"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment