Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:16:05+00:00 2026-06-10T18:16:05+00:00

models: class Person < ActiveRecord::Base attr_accessible :email, :first, :last, :uuid, :books has_many :books end

  • 0

models:

class Person < ActiveRecord::Base
  attr_accessible :email, :first, :last, :uuid, :books
  has_many :books
end

class Book < ActiveRecord::Base
  belongs_to :person
  attr_accessor :blurb, :published, :title, :person
  attr_accessible :person
end

I created a Person using the Rails 3.2.8 console like this:

person = Person.create!( {:first => "John", :last => "Doe"} )

and then created a Book

book = Book.create!( {:title => "Ruby for Dummies"} )

I then try to associate them like this:

person.books << book

When I query the person for books, I get an array with the book I created, but when I query the book for the person it belongs to, I get nil. I expected to get the person, given that all information was persisted (I see the SQL commands and I checked the database and the data is correct, i.e. the row in the book table points back to the person id it should.)

What am I missing?

thanks

Edit- Schema:

CREATE TABLE `persons` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `first` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `last` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `uuid` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `email` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `created_at` DATETIME NOT NULL,
    `updated_at` DATETIME NOT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2;

CREATE TABLE `books` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `blurb` TEXT NULL COLLATE 'utf8_unicode_ci',
    `published` TINYINT(1) NULL DEFAULT NULL,
    `person_id` INT(11) NULL DEFAULT NULL,
    `created_at` DATETIME NOT NULL,
    `updated_at` DATETIME NOT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:16:07+00:00Added an answer on June 10, 2026 at 6:16 pm

    Assuming your migrations are correct, there is nothing wrong with your code which should produce the following results:

    1.9.3p194 :001 > person = Person.create!( {:first => "John", :last => "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
     => #<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"> 
    1.9.3p194 :005 > book = Book.create!( {:title => "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
     => #<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"> 
    1.9.3p194 :006 > person.books << 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
     => [#<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">] 
    1.9.3p194 :007 > Book.all
      Book Load (0.5ms)  SELECT "books".* FROM "books" 
     => [#<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">] 
    

    The problem is with your Book model, try the following:

    class Book < ActiveRecord::Base
      attr_accessible :person, :title, :person
    
      belongs_to :person
    end
    

    Before:

    1.9.3p194 :002 > Book.last
      Book Load (0.1ms)  SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
     => #<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"> 
    1.9.3p194 :003 > Book.last.person
      Book Load (0.2ms)  SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
     => nil 
    

    After:

    1.9.3p194 :001 > Book.last
      Book Load (0.4ms)  SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
     => #<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"> 
    1.9.3p194 :002 > 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
     => #<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"> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: Company and Person class Person < ActiveRecord::Base belongs_to :company end
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I'm working on an association between two models: class Person < ActiveRecord::Base belongs_to :user
My Customer and Person models looks like this: class Customer < ActiveRecord::Base belongs_to :person
Here's my data models class Person < ActiveRecord::Base end class Landlord < Person end
So suppose I have the Person and Child models: class Person < ActiveRecord::Base has_many
Assume a standard has_many :through relationship among three models class Person < ActiveRecord::Base has_many
This are my models: class Speaker < ActiveRecord::Base belongs_to :session, :foreign_key => :session_id, :class_name
I have the following two models class ContactField < ActiveRecord::Base end class Address <
If I have a Model like the following example class Person < ActiveRecord::Base has_many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.