Skip to content

Instantly share code, notes, and snippets.

@jlogsdon
Created March 16, 2012 17:00
Show Gist options
  • Save jlogsdon/2051121 to your computer and use it in GitHub Desktop.
Save jlogsdon/2051121 to your computer and use it in GitHub Desktop.
user.profile.detail = {:education=> {:degree=>[], :specialization=>[], :institution=>[]}}
=begin
Now I am trying to store it in arrays inside hashes while migrating but when I view it it will be #difficult to show the contents in order since these 3 different values are being stored in 3 different #array inside hash.
Considering there could be multiple rows for a single user, I wanna store it in the ascending order of dates ie first course should be entered first although I dont want to migrate start and end date.
How can I have a better structure of storing the values
I want to migrate data into this hash structure for the give table structure.
ID DEGREE SPECIFICATION INSTITUTION START_DATE END_DATE
1 SSC Others A College 5/4/1999 4/4/2000
1 BA Mechanical B College 4/1/2002 5/1/2006
1 STANDARD XII Science C School 6/22/2000 3/1/2002
2 BA COMPUTER SCIENCE D COllege 8/2/2000 5/28/2003
3 SSC Others Other 6/2/1997 6/26/1998
3 STANDARD XII Maths College 8/2/1998 5/26/2000
My current rake task for migrating the data is as given below:
=end
class UserProfileMigrator
COL_USER = 0
COL_DEGREE = 1
COL_SPECIAL = 2
COL_INSTITUTE = 3
COL_STARTED = 4
COL_ENDED = 5
def initialize(csv_path, delimiter=',')
@csv_path = csv_path
@delimiter = delimiter
end
def process
CSV.foreach(@csv_path, {:col_sep => @delimiter}) do |row|
user = User.find_by_employee_id(row[COL_USER])
next if user.blank?
profile = user.profile
details = {
degree: (profile.detail[:education][:degree] || []) + row[COL_DEGREE],
specialization: (profile.detail[:education][:specialization] || []) + row[COL_SPECIAL],
institution: (profile.detail[:education][:institution] || []) + row[COL_INSTITUTION],
visible: false
}
profile.detail[:education] = details
if user.profile.save
puts "Successfully updated #{user.employeed_id}" unless SILENT
else
puts "Failed to update #{user.employeed_id}" unless SILENT
end
break # This will only process the first successful row
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment