0

When I try to upload a csv file I get the following error message:

ActiveModel::UnknownAttributeError in KontoumsatzsController#import
unknown attribute 'weg;wertstellung;umsatzart;buchungsdetails;auftraggeber;empfaenger;betrag;saldo' for Kontoumsatz.

My model:

class Kontoumsatz < ApplicationRecord
    attr_accessor :weg, :wertstellung, :umsatzart, :buchungsdetails, :auftraggeber, :empfaenger, :betrag, :saldo

    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            Kontoumsatz.create! row.to_hash
        end
    end

end

My controller:

  def import
    Kontoumsatz.import(params[:file])
    redirect_to kontoumsatzs_path, notice: "Erfolgreich importiert"
  end

Schema table:

create_table "kontoumsatzs", force: :cascade do |t|
    t.integer  "weg"
    t.string   "wertstellung"
    t.string   "umsatzart"
    t.string   "buchungsdetails"
    t.string   "auftraggeber"
    t.string   "empfaenger"
    t.decimal  "betrag"
    t.decimal  "saldo"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end

My routes:

  resources :kontoumsatzs do 
    collection { post :import }
  end

The file I am trying to upload is a CSV UTF-8(comma delimited) (.csv) file.

row.to_hash does not seem to work.

Any help would be appreciated.

4
  • How does your CSV file looks like? Commented Oct 26, 2017 at 13:27
  • Nice. Everything in german :) Commented Oct 26, 2017 at 13:32
  • @Rohit I cannot get it displayed here nicely, but it is an excel sheet saved as an csv file. When I try to copy/paste the data it does not show any comma's. Don't know if that makes a difference :) Commented Oct 26, 2017 at 13:43
  • @Fabrizio not my native language, so spelling mistakes are easy to make ;) Commented Oct 26, 2017 at 13:43

1 Answer 1

1

CSV - comma separated values. Comma in it stands for ',' ;)

So if you are going to use a delimiter other than a comma, you need to specify it. We do this using the col_sep option passed for the foreach of the CSV.parse. Its like this...

class Kontoumsatz < ApplicationRecord
    attr_accessor :weg, :wertstellung, :umsatzart, :buchungsdetails, :auftraggeber, :empfaenger, :betrag, :saldo

    def self.import(file)
        CSV.foreach(file.path, headers: true, col_sep: ';') do |row|
            Kontoumsatz.create! row.to_hash
        end
    end

end

This should help you parse your CSV file delimited using ;.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Sometimes it is just too easy. haha. All working now.
I know, made it many times my self, thats why I saw it straight away :P
I checked the ruby documentation of this CSV class and I really wonder why they don't explain this clearly ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html
I know it from experience, I think I'd used it like 20 times in the last 6 years as a Ruby dev. But I just checked the doc and in the second paragraph it says it "For example, :col_sep, :row_sep, and :quote_char must be transcoded to match your data"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.