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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:20+00:00 2026-06-17T16:58:20+00:00

Possible Duplicate: Correct Way to Set Default Values in Rails I have two data

  • 0

Possible Duplicate:
Correct Way to Set Default Values in Rails

I have two data tables:

1) User

2) Profile (which has the field user_id)

They are associated through:

  • User has_one Profile
  • Profile belongs_to User

Is there a possibility to save some default values in the profile table each time I create a new user?

Thanks for your help!

  • 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-17T16:58:21+00:00Added an answer on June 17, 2026 at 4:58 pm

    You can create default profile by using ActiveRecord callbacks.

    Just create a method, and use it as :after_create

    class User < ActiveRecord::Base
    
      has_one :profile
    
      after_create :create_default_profile
    
      def create_default_profile
        profile = build_profile
        # set parameters
        profile.save
      end
    
    end
    

    build_profile builds and links an instace of Profile, but doesn’t save it. create_profile is the same, but it also saves the object. For the full description, see the ActiveRecord documentation.

    You can add attributes to both build_ and create_profile as a hash, so you probably can reduce create_default_profile to one line:

    def create_default_profile
      profile = create_profile :some => 'attirbute', :to => 'set'
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the most accurate way to retrieve a user's correct IP
Possible Duplicate: What is the most accurate way to retrieve a user's correct IP
Possible Duplicate: Correct way of using JQuery-Mobile/Phonegap together? do I have to install jquery
Possible Duplicate: Fastest Way to Find Distance Between Two Lat/Long Points I have locations
Possible Duplicate: Correct way to escape input data before passing to ODBC the error
Possible Duplicate: Which is more correct: <h1><a>..</a></h1> OR <a><h1>..</h1></a> Semantically, which is more correct:
Possible Duplicate: What is the best way to store user settings for a .NET
Possible Duplicate: Correct way to use scanf / printf (and family) with fixed size
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: What is the correct way of using extern for global variables ?

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.