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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:40:21+00:00 2026-05-11T10:40:21+00:00

How do the relationships magically function when only the models are altered? If I

  • 0

How do the relationships magically function when only the models are altered?

If I want a ‘has__and___belongs___to__many’ relationship, what should I name the table (so Rails can use it) that contains the two foreign keys?

  • 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. 2026-05-11T10:40:22+00:00Added an answer on May 11, 2026 at 10:40 am

    Short answer: You can’t just tell the models that they’re related; there have to be columns in the database for it too.

    When you set up related models, Rails assumes you’ve followed a convention which allows it to find the things you wrote. Here’s what happens:

    1. You set up the tables.

      Following conventions in Rails, you name the table in a particular, predictable way (a plural noun, e.g. people). In this table, when you have a relationship to another table, you have to create that column and name it in another predictable way (e.g. bank_account_id, if you’re relating to the bank_accounts table).

    2. You write a model class inheriting from ActiveRecord::Base

      class Person < ActiveRecord::Base

      When you instantiate one of these models, the ActiveRecord::Base constructor looks at the name of the class, converts it to lowercase and pluralizes it. Here, by reading Person, it yields people, the name of the table we created earlier. Now ActiveRecord knows where to get all the information about a person, and it can read the SQL output to figure out what the columns are.

    3. You add relationships to the model: has_many, belongs_to or has_one.

      When you type something like, has_many :bank_accounts, it assumes a few things:

      1. The name of the model that you relate to is BankAccount (from camel-casing :bank_accounts).

      2. The name of the column in the people table which refers to a bank account is bank_account_id (from singularizing :bank_accounts).

      3. Since the relationship is has_many, ActiveRecord knows to give you methods like john.bank_accounts, using plural names for things.

    Putting all of that together, ActiveRecord knows how to make SQL queries that will give you a person’s bank accounts. It knows where to find everything, because you followed a naming convention that it understands when you created the table and its colums.

    One of the neat things about Ruby is that you can run methods on a whole class, and those methods can add other methods to a class. That’s exactly what has_many and friends are doing.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It depends whether the list of the added functionlities is… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer CREATE PROCEDURE UpdateTables AS BEGIN -- SET NOCOUNT ON added… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer I've managed to simulate this situation: To do a normal… May 11, 2026 at 11:44 pm

Related Questions

Several of the resources I've gone to on the internet have disagree on how
How do I use the Inverse Attribute? If I am not mistaken, for one
I'm having problems querying the entity model to get additional information. My db has
Almost every Java book I read talks about using the interface as a way

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.