The hash that row.to_hash
gives you will look like this:
{ 'Name' => 'Mr. Foo', 'Email' => 'foo@bar.com', ... }
The problem here is that the keys are strings that are capitalized. 'Email' != 'email'.
If you are passing the hash directly to create!
, it needs to conform to the attribute names exactly, either as strings or symbols:
{ 'name' => 'Mr. Foo', :email => 'foo@bar.com', ... }
You should sanitize the keys to be snake_case (the same as their respective accessible attributes) before passing them to create!
, or change them in the CSV.
You might also consider assigning each attribute individually from the row hash, and assigning a default value if it a value is not found.
hash = row.to_hash
judge = Judge.new
judge.name = hash['Name'] || hash['name'] || 'No name'
judge.email = hash['Email'] || hash['email']
judge.competition_id = hash['Competition_ID'] || hash['competition_id']
This is probably not the best thing to do in this case, but it can make your import script be much more robust if you are not in strict control over the format of the CSV files. It also gives you the opportunity to sanitize the values before writing them into your database.