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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:25:28+00:00 2026-06-17T14:25:28+00:00

I’m creating a relationship between a user and its profile like this in Rails

  • 0

I’m creating a relationship between a user and its profile like this in Rails

User has_one :profile
Profile belongs_to :user

I understand that under normal circumstances Rails automatically creates the foreign key relationship between those two models. However, when I am seeding the database with the information in the following way, with many Users created, and many Profiles created, then how do I ensure, for example, that Joe’s profile is linked with Joe’s user account.

User.create!(email: "j@gmail.com", password: "blahblah", encryptedpass: "3838")
Profile.create!(first_name: "Joe", last_name: "Frank", address: "43 Flint Road", phone: "604 345 678", )

 User.create!(email: "anne@gmail.com", password: "blahblahblah", encryptedpass: "4567")
Profile.create!(first_name: "Anna", last_name: "Jones", address: "43 Boogy Road", phone: "604 345 678", )

…imagine thousands of users and profiles being seeded in this way….

Do i have to set a user_id foreign key on profile and, for the purposes of seeding it, do I just make up a foreign key id? Can rails generate it for me? If for example, I made a foreign key on Joe’s profile as user_id: 1 how can I be sure that his instance of the user model will be “1” In other words, if I manually set his user_id foreign key, how can I make sure it’ll match the id of the User model?

  • 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-17T14:25:29+00:00Added an answer on June 17, 2026 at 2:25 pm

    You can use create_profile! method (which is created by Rails when you declare has_one :profile

    u = User.create!(email: "j@gmail.com", password: "blahblah", encryptedpass: "3838")
    u.create_profile!(first_name: "Joe", last_name: "Frank", address: "43 Flint Road")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I

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.