pcreux/csv-importer

Importer class never found

Closed this issue · 5 comments

I couldn't get this to work in a reasonable amount of time, so I was wondering where you typically place your importer files in a Rails application.

I tried putting my ImportItemsCSV in the models directory but even then I end up getting NoMethodError - undefined methodnew' for nil:NilClass:` when I try to instantiate it in a controller.

You should be doing something wrong. Check again the file name and file syntax,

Read the manuals:

class ImportUserCSV
include CSVImporter

model User # an active record like model

column :email, to: ->(email) { email.downcase }, required: true
column :first_name, as: [ /first.?name/i, /pr(é|e)nom/i ]
column :last_name,  as: [ /last.?name/i, "nom" ]
column :published,  to: ->(published, user) { user.published_at = published ? Time.now : nil }

identifier :email # will update_or_create via :email

when_invalid :skip # or :abort
end

Ensure you have specified a model class, like: model User

ACPK commented

@tirdadc - I agree with you that the Readme misses a few steps / can be hard to follow. Did you get it working as @pcreux closed it but I don't see an update?

@ACPK Are you running into a similar issue? If you use Rails, any class defined in a file (with matching name) will be loaded from any directory under app.

An importer called "ImportUserCSV" can be defined in app/models/import_user_csv.rb (or app/importers/import_user_csv.rb....)

All the details are here: http://guides.rubyonrails.org/autoloading_and_reloading_constants.html

ACPK commented

@pcreux Couple so moved to smarter_csv after the issues kept getting closed.

@ACPK I didn't think someone would move to another library because the maintainer closes issues after he has responded. :) Enjoy smarter_csv!