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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:22:58+00:00 2026-05-18T07:22:58+00:00

In my Ruby on Rails application I want to create a new profile and

  • 0

In my Ruby on Rails application I want to create a new profile and a new statistic for profile, all calling first the related method from the user model, and then from the profile model.

So…

… in my user model (user.rb) I have this:

...

has_one :profile

...
before_save :inizialize_user
...

private

def inizialize_user
  @user_profile = Profile.new
  self.user_profile_id = @user_profile.id
end

… in my profile model (profiles.rb) I have this:

...
belongs_to :user
...

before_save :inizialize_profile

private

def inizialize_profile
  @profile_statistic = ProfileStatistic.new
end

In the second block of code, on the “before_save” it instantiates a new profile statistic:
“inspecting” @profile_statistic results a new object (correct!)

In the first block of code, on the “before_save” it doesn’t instantiate a new profile:
“inspecting” the @user_profile results nil (it must be a new profile object!)

The last part is my problem. Why it happens?

  • 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-18T07:22:58+00:00Added an answer on May 18, 2026 at 7:22 am

    When you call Profile.new, it only creates an instance in memory, it isn’t saved to the database and therefore doesn’t have an id attribute (i.e. @user_profile.id is nil).

    I suggest you replace

    @user_profile = Profile.new
    

    with

    @user_profile = Profile.create
    

    create will save the instance and then @user_profile.id will not be nil.

    You probably also wan to use before_create callbacks (not before_save), or you’ll have new user profiles every time you save the model (e.g. after udpating). Also, you probably want to have

    ProfileStatistic.create
    

    instead of

    ProfileStatistic.new
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to log user's actions in my Ruby on Rails application. So far,
I'm about to build my first Ruby on Rails application (and first anything beyond
After our Ruby on Rails application has run for a while, it starts throwing
I am working on the billing component of a Ruby on Rails application using
Imagine a web application written in Ruby on Rails. Part of the state of
I'm creating a fast application with ruby on rails, and after doing a lot
We are developing a considerably big application using Ruby on Rails framework (CRM system)
I'm building an iPhone application that talks to a Ruby on Rails backend. The
I'm working on my first Ruby on Rails aplication and it's quite big (at
Within Ruby on Rails applications database.yml is a plain text file that stores database

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.