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

  • Home
  • SEARCH
  • 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 7921365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:33:25+00:00 2026-06-03T16:33:25+00:00

I have 2 tables: contacts and users (Devise). I would like to create a

  • 0

I have 2 tables: contacts and users (Devise).
I would like to create a user each time an contact is created.
The contact belongs_to :user
The user has_one :contact

The following code in the contact.rb file creates the user when you add a new contact:

before_create :create_user

protected
def create_user
  self.user = User.new({ :email => self.email, :password => '123456' })
  return user.save
end

But, the contact field user_id (foreign key) isn’t getting updated with the new user id.
How can accomplish that?

Thanks!

  • 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-03T16:33:27+00:00Added an answer on June 3, 2026 at 4:33 pm

    Try this instead

    def create_user
      self.user = User.create({ :email => self.email, :password => '123456' })
    end
    

    My guess is that the association is not being registered because after the User.new call the object has not been saved to the database and has not been assigned an id attribute, and the id attribute is what is required to record the association. User.create initializes the object and saves it to the database in one step (assuming all validations pass).

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

Sidebar

Related Questions

I have two tables named users and contacts . I would like to delete
I have created some tables with text fields inside, like in the contacts for
This question would be a bit childish, I have three tables, users, usergroups and
Say I have four tables, users , contacts , files , and userfiles .
Let's say I have 2 tables, Contacts and Phones . User can input name
I have bassicly 3 tables : Users (email, password), Contacts (name, phone), Relations (user_id,
I have two tables, in which one user can have several contacts: User(UserId, UserName)
I have three tables, one listing contacts, the other users, and the last emails.
I have multiple user account types, each with different information. For example, business contacts
I have a few tables which I have joined together and would like to

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.