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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:51:18+00:00 2026-05-17T17:51:18+00:00

In the Rails ActiveRecord Associations guide, I’m confused over why the tables for has_one

  • 0

In the Rails ActiveRecord Associations guide, I’m confused over why the tables for has_one and has_many are identical:

Example tables for has_many:

customers(id,name)
orders(id,customer_id,order_date)

Example tables for has_one:
these tables will, at the database level, also allow a supplier to have many accounts, but we just want one account per supplier

suppliers(id,name)
accounts(id,supplier_id,account_number) #Foreign Key to supplier here??

Shouldn’t the tables for has_one be like this instead:

suppliers(id,name,account_id) #Foreign Key to account here
accounts(id,account_number)

Now because the account_id is in the suppliers table, a supplier can never have more than one account.

Is the example in the Rails Guide incorrect?

Or, does Rails use the has_many kind of approach but restricts the many part from happening?

  • 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-17T17:51:19+00:00Added an answer on May 17, 2026 at 5:51 pm

    If I understand your question correctly, you believe there is a 1:1 relationship bi-directionally in a has_one/belongs_to relationship. That’s not exactly true. You could have:

    Class Account
      belongs_to :supplier
      belongs_to :wholesaler
      belongs_to :shipper
      # ...
    end
    
    account = supplier.account       # Get supplier's account
    wholesaler = Wholesaler.new
    wholesaler.accounts << account   # Tell wholesaler this is one of their suppliers
    wholesaler.save
    

    I’m not saying your app actually behaves this way, but you can see how a table — no, let’s say a model — that “belongs to” another model is not precluded from belonging to any number of models. Right? So the relationship is really infinity:1.

    I should add that has_one is really a degenerate case of has_many and just adds syntactic sugar of singularizing the association and a few other nits. Otherwise, it’s pretty much the same thing and it’s pretty much why they look alike.

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

Sidebar

Related Questions

In ActiveRecord, has_many :through, and Polymorphic Associations , the OP's example requests ignoring the
From the Rails associations guide, they demonstrate a many-to-many relationship using has_many :through like
In my Rails application there is a model that has some has_one associations (this
I have the following :has_many :through relation. Associations class Profile < ActiveRecord::Base has_many :teams
I have the usual polymorphic associations for comments: class Book < ActiveRecord::Base has_many :comments,
I have three classes with the following associations: class Technician < ActiveRecord::Base has_many :tickets
Using Rails ActiveRecord, am wanting to calculate the time between two datetime fields within
I have an Rails ActiveRecord group query: a = Product.select(date(date) as date, count(id) as
I've added a before_create filter to one of my Rails ActiveRecord models and inside
I'm working with an older version of Rails and ActiveRecord that does not have

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.