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:
first_name
and last_name
columns to the User model.first_name
and last_name
value for a given user from the associated Profile record and copy it into the User model.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!
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With