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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:49:22+00:00 2026-06-04T13:49:22+00:00

Consider the two models: User => (id: integer, name: string, created_at: datetime, updated_at: datetime,

  • 0

Consider the two models:

  1. User => (id: integer, name: string, created_at: datetime, updated_at: datetime, department_id: integer)
  2. Department => (id: integer, name: string, created_at: datetime, updated_at: datetime)

Now, there is a obvious relationship between these two tables. it is

  • Each User has one department
  • A Department belongs to many users i.e there are multiple users in a single department

so, I chose epress the same as following as A belongs_to B

class User < ActiveRecord::Base
  has_one :department
end
class Department < ActiveRecord::Base
  belongs_to :user
end

But as you may know doesn’t work & throws the following error:

> @user = User.find(1)
> @user.department.name
  Department Load (1.0ms)  SELECT "departments".* FROM "departments" WHERE "departments"."user_id" = 1 LIMIT 1
ActiveRecord::StatementInvalid: PG::Error: ERROR:  column departments.user_id does not exist

After lot of hit & trial. I stumbled on the correct way to define this, which is just other way around i.e B belongs to A

class Department < ActiveRecord::Base
  has_many :users
end
class User < ActiveRecord::Base
  belongs_to :department
end
> @user = User.find(1)
> @user.department.name
Department Load (1.0ms)  SELECT "departments".* FROM "departments" WHERE "departments"."id" = 1 LIMIT 1
 => "HR"

Now, this is exact opposite of the way my brain thinks about these associations. So, I am little confused, so if anyone could explain what is going on?

Why does B belongs to A & NOT A belongs to B?

  • 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-04T13:49:23+00:00Added an answer on June 4, 2026 at 1:49 pm

    The :has_one relation is a little bit strange at the first look. Consider a user and an account, every account is connected to (belongs to) just one user, from the other side a user has_one account, just one, his account. An account is something that the user owns.

    In these cases you use the has_one relation on the User model and Rails will search for a user_id column in the accounts table (see the has_one documentation in the Rails Guide).

    In your specific case I think is perfecty right to say “a department has many users” and “a user belongs to a department”.

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

Sidebar

Related Questions

The problem is best explained by example, consider the following two models: class Topping(models.Model):
Let's consider the following table models for sqlalchemy in python. class Worker(Base): id Column(Integer,
Which of these two different models would be more efficient (consider thrashing, utilization of
Consider these two scenarios: a user presses a button in a view (e.g. Fulfill
Consider two references of type Integer that call the static factory method valueOf as
Consider two tables: Transactions , with amounts in a foreign currency: Date Amount =========
Consider two methods on the controller CustomerController.cs : //URL to be http://mysite/Customer/ public ActionResult
Consider two dates 2010-03-18 22:30:45 and 2010-03-19 03:30:15 .... How to get the number
Consider these two classes mapped to the same table. One is readonly via mutable=false.
Consider the two vectors: v= [1 2 3 4 5 6 7] a=['a' 'b'

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.