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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:57:57+00:00 2026-05-22T15:57:57+00:00

My Rails 3 app has a User model and a Profile model. A User

  • 0

My Rails 3 app has a User model and a Profile model. A User has_one Profile.

Currently the Profile has attributes first_name and last_name. While I’m not sure why you might want to change them, I originally imagined these should be changeable and so I put them in the Profile model instead of the User model.

However, as the app has evolved I have found that in fact I need the user’s first and last name not to be changed, and that I really need them to be part of the User model instead of the profile model.

So, I was wondering, can you write a migration that would:

  1. Add the first_name and last_name columns to the User model.
  2. Take the existing first_name and last_name value for a given user from the associated Profile record and copy it into the User model.
  3. Remove the first_name and last_name columns from the Profile model since they wouldn’t be needed anymore.

So, can this be done? Could you show an example? And, most importantly, are there any gotchas I should be aware of? I’d like to apply this change to a production app so it’s critical that I don’t lose data when I make this change.

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-05-22T15:57:58+00:00Added an answer on May 22, 2026 at 3:57 pm

    Sure, very easy. Put this in a migration:

        add_column(:users, :last_name, :string)
        add_column(:users, :first_name, :string)
    
        User.reset_column_information
    
        User.all.each do |u|
          u.last_name = u.profile.try(:last_name)
          u.first_name = u.profile.try(:first_name)
          u.save
        end
    
        remove_column(:profiles, :first_name)
        remove_column(:profiles, :last_name)
    

    I used try() there to mitigate against the possibility of a missing profile. It’s not checking for errors on the save operation, so take that into account if you think it needs to. Also, as always, do a database dump backup before you run it. 🙂

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

Sidebar

Related Questions

My rails app has a simple model Links which tracks all the Urls associated
My Rails-app has a sign in box with a remember me checkbox. Users who
I have a rails app that has picked up a bit of traction, but
I'm working on a rails app that has a whole bunch of before filters
I need to store app specific configuration in rails. But it has to be:
I would like to show each user who has visited her/his user profile. At
I'm building a Rails app that has Etsy.com style functionality. In other words, it's
Basically, I have a users model in my rails app, and a fanship model,
My Rails 3 app has 2 models and a third that's join table between
We have already built a rails app that has several users and an image

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.