Hey I am trying to import one excel file from my sheet to database but I don't know something is not working getting unknown attribute :
My sheet is like this :
name title jalebi samosa s1 s2 s3
1 4 7 7 7 7 7
2 5 6 6 6 6 6
3 6 5 5 5 5 5
4 7 4 4 4 4 4
And my Migration is :
class CreateTest3s < ActiveRecord::Migration[5.1]
def change
create_table :test3s do |t|
t.string :name
t.string :title
t.string :jalebi
t.string :samosa
t.string :s1
t.string :s2
t.string :s3
t.timestamps
end
end
end
And my import code in model is :
def self.import1(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
puts row.to_hash
product = find_by(id: row["id"]) || new
product.attributes = row.to_hash
product.save!
end
end
Whenever I am trying to run this I am getting this error:
unknown attribute 'samosa ' for Test3.
and the value of hash is like this :
{"name"=>1, "title"=>4, "jalebi"=>7, "samosa "=>9, "s1"=>7, "s2"=>7, "s3"=>7}
It looks like the attribute you are trying to set is "samosa "
with a trailing space. The attribute should be just "samosa"
with no whitespace. I'm guessing your spreadsheet header is where the space is coming from.