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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:10:23+00:00 2026-05-26T19:10:23+00:00

I am working on a Rail webapp. I have two models, User, which contains

  • 0

I am working on a Rail webapp. I have two models, User, which contains very basic information: id, username and password, and Profile, which includes profile for each user. (The main reason is to have a lightweight User model, which will be called regularly, and a full-fledged profile which will be called irregularly). Each of these models has many children.

Right now, I have Profile with its own primary key, then a foreign key user_id to match with User.

However, I wonder if I should have Profile model with the same key as User model (i.e., Profile.id == User.id if the records refer to the same user). This is convenience because when I have an object that belongs to User, I want it to belong to Profile and vise versa. For example, I can specify User has_many and Spec has_many relationship to ChildModel. Because they use the same key, I don’t have to merge ChildModel to Spec, then Profile to User to find out user associated with child object.

The downside is in the future, if for some reason I have discrepancy between primary key of User and Spec, then I am in deep trouble.

What would be your recommendation for this situation?

Thank you.

  • 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-26T19:10:23+00:00Added an answer on May 26, 2026 at 7:10 pm

    It sounds like you should be using a one-to-one relationship between Profile and User. You can create this using the has_one and belongs_to declarations.

    class User < ActiveRecord::Base
      has_one :profile
    end
    
    class Profile < ActiveRecord::Base
      belongs_to :user
    end
    

    As stated in Agile Web Development with Rails Fourth Edition:

    There’s an important rule illustrated here: the model for the table that contains the foreign key always has the belongs_to declaration.

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

Sidebar

Related Questions

Working on an old Kohana 2 project and I want to link two models.
Working with some basic java apps on CentOS 5 linux and I have my
I have been working with Rail 3.1 and just noticed when I turn on
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project at the moment and we have to implement soft deletion
Working with python interactively, it's sometimes necessary to display a result which is some
Working in the JBoss AS portlet we have been unable to open a jsp
I am working on a webserver which is a third party so i can
I'm new to Ruby on Rails, trying to get my first app working, which
Working SQL The following code works as expected, returning two columns of data (a

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.