Skip to content

Instantly share code, notes, and snippets.

@slothbear
Created November 22, 2011 02:41
Show Gist options
  • Select an option

  • Save slothbear/1384738 to your computer and use it in GitHub Desktop.

Select an option

Save slothbear/1384738 to your computer and use it in GitHub Desktop.
rails console test of belongs_to association
ruby :032 > site = Site.create(:name => "Boston")
SQL (0.5ms) INSERT INTO "sites" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Tue, 22 Nov 2011 02:28:57 UTC +00:00], ["name", "Boston"], ["updated_at", Tue, 22 Nov 2011 02:28:57 UTC +00:00]]
=> #<Site id: 5, name: "Boston", created_at: "2011-11-22 02:28:57", updated_at: "2011-11-22 02:28:57">
ruby :033 > device = Device.create(:name => "hackatron")
SQL (0.6ms) INSERT INTO "devices" ("created_at", "name", "site_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 22 Nov 2011 02:29:13 UTC +00:00], ["name", "hackatron"], ["site_id", nil], ["updated_at", Tue, 22 Nov 2011 02:29:13 UTC +00:00]]
=> #<Device id: 5, name: "hackatron", created_at: "2011-11-22 02:29:13", updated_at: "2011-11-22 02:29:13", site_id: nil>
ruby :034 > site.devices << device
(0.4ms) UPDATE "devices" SET "site_id" = 5, "updated_at" = '2011-11-22 02:29:39.327808' WHERE "devices"."id" = 5
Device Load (0.2ms) SELECT "devices".* FROM "devices" WHERE "devices"."site_id" = 5
=> [#<Device id: 5, name: "hackatron", created_at: "2011-11-22 02:29:13", updated_at: "2011-11-22 02:29:39", site_id: 5>]
ruby :035 > device.site.name
Site Load (0.2ms) SELECT "sites".* FROM "sites" WHERE "sites"."id" = 5 LIMIT 1
=> "Boston"
ruby :036 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment