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 6334865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:44:30+00:00 2026-05-24T18:44:30+00:00

class User < ActiveRecord::Base has_many :boxes has_many :books end class Box < ActiveRecord::Base belong_to

  • 0
class User < ActiveRecord::Base
    has_many :boxes
    has_many :books
end

class Box < ActiveRecord::Base
    belong_to :user
    has_many :books
end

class Book < ActiveRecord::Base
    belongs_to :user
    belongs_to :box
end

So when I run below in a console it works as I expect, creating a new box and attaching the user_id to the box

>> u = User.first
>> u.boxes.build(:height => 3, :width => 1, :length => 4)
>> u.save

So then I go further and attempt this. The box_id is set, but the user_id in books is not set.

>> u.boxes.first.books.build(:title => 'Reading is fun')
>> u.save

It seems like I’m missing a pretty fundamental concept here.

  • 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-05-24T18:44:31+00:00Added an answer on May 24, 2026 at 6:44 pm

    Each relationship is distinct from all the others. That is, when you build a Book belonging to u.boxes.first, all Rails infers is that the Book belongs to that Box; it says nothing about the User.

    In this situation, where (presumably) Books are always owned by the person who owns the Box they’re in, you’re probably best off with a has_many :through relationship:

    class User < ActiveRecord::Base
        has_many :boxes
        has_many :books, :through => :boxes
    end
    
    class Box < ActiveRecord::Base
        belong_to :user
        has_many :books
    end
    
    class Book < ActiveRecord::Base        
        belongs_to: box
        delegate :user, :to => :box # since belongs_to doesn't take :through
                                    # passes Book#user through to Book#box.user
    end
    

    If that’s not the case (perhaps you’re helping me move?), leave the associations as they are, but you’ll have to set at least one of the relations up manually, for instance:

    b = u.boxes.first.books.build(:title => "foo")
    b.user = u
    b.save
    
    # or...
    u.boxes.first.create_book(:title => "foo", :user => u)
    

    (Couple of other points. In your second example you should save the Book, not the User; and you can do both build and save in a single operation with create)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Exercise < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :exercises end
class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end
class Membership < ActiveRecord::Base belongs_to :role belongs_to :user end class User < ActiveRecord::Base has_many
I have class Store < ActiveRecord::Base belongs_to :user has_many :products, :as => :imageable end
class Comment < ActiveRecord::Base belongs_to :post belongs_to :user end class Post < ActiveRecord::Base has_many
class User < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :user end
My code: class User < ActiveRecord::Base belongs_to :university end class University < ActiveRecord::Base has_many
I have this relationship : class Organization < ActiveRecord::Base has_many :users end class User
I am using Rails 2.3.5. Class User < ActiveRecord::Base has_many :phones end class Phone
I have 2 models: Video: class Video < ActiveRecord::Base belongs_to :user has_many :thumbnails attr_accessor

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.