I have a CSV of users to upload, with one of the headers named as country of origin of user. This should be mapped to a column in the users table called country_id during importing. country has_many users. I am struggling to import such kind of column headers in the csv which map to an association and which have funny names. Many days of trying but I am yet to crack it. Here is the method I am using to import data:
def load_imported_usersspreadsheet = open_spreadsheetheader = spreadsheet.row(1)(2..spreadsheet.last_row).map do |i|row = Hash[[header, spreadsheet.row(i)].transpose]user = User.find_by_national_id(row["national_id"]) || User.newuser.attributes = row.to_hashuserendend
Help would be appreciated, thanks!
I have a CSV of users to upload, with one of the headers named as country of origin of user. This should be mapped to a column in the users table called
country_id
during importing.country has_many users
. I am struggling to import such kind of column headers in the csv which map to an association and which have funny names. Many days of trying but I am yet to crack it. Here is the method I am using to import data:def load_imported_users
spreadsheet = open_spreadsheet
header = spreadsheet.row(1)
(2..spreadsheet.last_row).map do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
user = User.find_by_national_id(row["national_id"]) || User.new
user.attributes = row.to_hash
user
end
end
Help would be appreciated, thanks!