Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming your migrations are correct, there is nothing wrong with your code which should produce the following results:</p> <pre><code>1.9.3p194 :001 &gt; person = Person.create!( {:first =&gt; "John", :last =&gt; "Doe"} ) (0.1ms) begin transaction SQL (145.9ms) INSERT INTO "people" ("book_id", "created_at", "email", "first", "last", "updated_at", "uuid") VALUES (?, ?, ?, ?, ?, ?, ?) [["book_id", nil], ["created_at", Mon, 03 Sep 2012 21:35:53 UTC +00:00], ["email", nil], ["first", "John"], ["last", "Doe"], ["updated_at", Mon, 03 Sep 2012 21:35:53 UTC +00:00], ["uuid", nil]] (43.5ms) commit transaction =&gt; #&lt;Person id: 1, email: nil, first: "John", last: "Doe", uuid: nil, book_id: nil, created_at: "2012-09-03 21:35:53", updated_at: "2012-09-03 21:35:53"&gt; 1.9.3p194 :005 &gt; book = Book.create!( {:title =&gt; "Ruby for Dummies"} ) (0.1ms) begin transaction SQL (1.4ms) INSERT INTO "books" ("blurb", "created_at", "person_id", "published", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["blurb", nil], ["created_at", Mon, 03 Sep 2012 21:36:25 UTC +00:00], ["person_id", nil], ["published", nil], ["title", nil], ["updated_at", Mon, 03 Sep 2012 21:36:25 UTC +00:00]] (4.4ms) commit transaction =&gt; #&lt;Book id: 1, blurb: nil, published: nil, title: nil, person_id: nil, created_at: "2012-09-03 21:36:25", updated_at: "2012-09-03 21:36:25"&gt; 1.9.3p194 :006 &gt; person.books &lt;&lt; book (0.1ms) begin transaction (0.3ms) UPDATE "books" SET "person_id" = 1, "updated_at" = '2012-09-03 21:36:39.566387' WHERE "books"."id" = 1 (4.4ms) commit transaction Book Load (0.3ms) SELECT "books".* FROM "books" WHERE "books"."person_id" = 1 =&gt; [#&lt;Book id: 1, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:36:25", updated_at: "2012-09-03 21:36:39"&gt;] 1.9.3p194 :007 &gt; Book.all Book Load (0.5ms) SELECT "books".* FROM "books" =&gt; [#&lt;Book id: 1, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:36:25", updated_at: "2012-09-03 21:36:39"&gt;] </code></pre> <p>The problem is with your Book model, try the following:</p> <pre><code>class Book &lt; ActiveRecord::Base attr_accessible :person, :title, :person belongs_to :person end </code></pre> <p><strong>Before</strong>:</p> <pre><code>1.9.3p194 :002 &gt; Book.last Book Load (0.1ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1 =&gt; #&lt;Book id: 4, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:55:33", updated_at: "2012-09-03 21:55:33"&gt; 1.9.3p194 :003 &gt; Book.last.person Book Load (0.2ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1 =&gt; nil </code></pre> <p><strong>After</strong>:</p> <pre><code>1.9.3p194 :001 &gt; Book.last Book Load (0.4ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1 =&gt; #&lt;Book id: 4, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:55:33", updated_at: "2012-09-03 21:55:33"&gt; 1.9.3p194 :002 &gt; Book.last.person Book Load (1.0ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1 Person Load (0.1ms) SELECT "people".* FROM "people" WHERE "people"."id" = 1 LIMIT 1 =&gt; #&lt;Person id: 1, email: nil, first: "John", last: "Doe", uuid: nil, created_at: "2012-09-03 21:48:02", updated_at: "2012-09-03 21:48:02"&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload