My table field names are lowercase and the field names I get from CSV files are camelcase. Is there anyway I can convert the keys of an array of hashes to lowercase?
Ruby 1.9 provides quite a nice way to achieve this, using each_with_object to avoiding initialising a temporary variable.
CSV.foreach(file, :headers => true) do |row| new_hash = row.each_with_object({}) do |(k, v), h| h[k.downcase] = v end Users.create!(new_hash) end