During development, structure of column has been changed, so it's needed to adopt old users data to new format in production. It looks like issue that can be solved by migration. The problem is, I'm not an experienced ruby specialist, so it would be great to have advice how to implement it.
To make things clear, I'll give an example of what happened in my project.
There is table users
. This table contains next columns,
id
user_type
description
description
here is just JSON string that looks like that in old implementation,
first_name
last_name
address
After changes, instead of first_name
and last_name
we have full_name
, only for users with type 'customer'.
So, how can I migrate my old data to new format? Thanks.
Your respective model User
must have following,
serialize :description, Hash
Try to write rake in below path,
lib/tasks/update_users.rake
namespace :update_users do
desc 'Update description for full name for all user'
task update_description: :environment do
User.all.each do |user|
user.description[:full_name] = user.description.delete(:first_name) + ' ' + user.description.delete(:last_name)
user.save(validate: false)
end
end
end
And run rake as, rake update_users:update_description
Perhaps you can run code through rails console,
User.all.each do |user|
user.description[:full_name] = user.description.delete(:first_name) + ' ' + user.description.delete(:last_name)
user.save(validate: false)
end