Created
February 9, 2014 15:35
-
-
Save Chryus/8900766 to your computer and use it in GitHub Desktop.
Access resources via has_many :through in rails 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
bunny2 = Bunny.find_by name: "Hakeem" | |
Bunny Load (0.2ms) SELECT "bunnies".* FROM "bunnies" WHERE "bunnies"."name" = 'Hakeem' LIMIT 1 | |
=> #<Bunny id: 13, name: "Hakeem", created_at: "2014-02-09 15:01:53", updated_at: "2014-02-09 15:01:53"> | |
2.0.0-p247 :091 > bunny2.carrot_ids | |
(0.1ms) SELECT "carrots".id FROM "carrots" INNER JOIN "communities" ON "carrots"."id" = "communities"."carrot_id" WHERE "communities"."bunny_id" = ? [["bunny_id", 13]] | |
=> [] | |
2.0.0-p247 :092 > bunny2.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", 13]] | |
(0.1ms) begin transaction | |
SQL (0.5ms) INSERT INTO "communities" ("bunny_id", "carrot_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["bunny_id", 13], ["carrot_id", 9], ["created_at", Sun, 09 Feb 2014 15:33:07 UTC +00:00], ["updated_at", Sun, 09 Feb 2014 15:33:07 UTC +00:00]] | |
SQL (0.1ms) INSERT INTO "communities" ("bunny_id", "carrot_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["bunny_id", 13], ["carrot_id", 10], ["created_at", Sun, 09 Feb 2014 15:33:07 UTC +00:00], ["updated_at", Sun, 09 Feb 2014 15:33:07 UTC +00:00]] | |
(8.0ms) commit transaction | |
=> [9, 10] | |
2.0.0-p247 :093 > bunny2.carrots | |
=> #<ActiveRecord::Associations::CollectionProxy [#<Carrot id: 9, color: "green", created_at: "2014-02-09 15:02:04", updated_at: "2014-02-09 15:02:04">, #<Carrot id: 10, color: "orange", created_at: "2014-02-09 15:02:09", updated_at: "2014-02-09 15:02:09">]> | |
2.0.0-p247 :094 > bunny2.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