user1185081
user1185081

Reputation: 2118

How to add temporary attributes to Ruby on Rails model during importation

I use the roo gem to import data from xlsx files. But in some cases, column available in the xlsx file will not match the target model. Some columns need concatenation or look-up ... there are more columns in the file than in the target model. Here is my code:

  def load_imported_skills
    #puts open_spreadsheet.nil?
    spreadsheet = self.open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).map do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      import = Skill.find_by_id(row["id"]) || Skill.new
      import.attributes = row.to_hash
      puts "test"
      puts import.attributes
      import
    end
  end

How can I temporary add virtual attributes to support actuel imput file structure and do some calculation before saving the processed record?

Thanks for help!

Upvotes: 1

Views: 154

Answers (0)

Related Questions