active_importer
active_importer copied to clipboard
Populate information without to specify all the column names
Hello Guys,
I'm wondering if is possible to populate the information without specifying one by one all the column names for example let's say I have a current CSV file with all the valid column names in place but some of the columns need special logic but for all the remaining columns names that appear in the CSV are exactly the same in the model but after to run the importer is only creating the models with the columns that I have specified in other words this is my data:
body; title; votes_count; image_url
some 1, title, 25, , http://example.com/image.jpg
some 2, title, 25, , http://example.com/image.jpg
some 3, title, 25, , http://example.com/image.jpg
# my model
Book(id: integer, title: string, votes_count: integer, body: text, image_file_name: string, image_content_type: string, image_file_size: integer, image_updated_at: datetime)
# My importer
class BookImporter < ActiveImporter::Base
imports Book
column 'image_url', :image do |image_url|
URI.parse(image_url)
end
end
After to run my importer the title, body, and votes_count are nil and only the image is populated correctly.
Any ideas about this behavior?, thanks in advanced guys, btw active importer it is a really good gem :)
I just made a fork with a small change where this is possible, it tries to match header names with model columns skipping the ones already defined. Maybe this behavior should be optional. https://github.com/grillermo/active_importer/commit/7070c02358fa559448bfbe6ceff35bafbead04ca
@grillermo you idea sounds good. I found a problem however, and I left a comment explaining it in the commit that holds your proposed changes.
I also mentioned there, and I think it should be mentioned here as well, that this feature should be enabled explicitly in the importer class, and not assumed that always all importers should use all the model's column names as importer columns. Perhaps something like this:
class EmployeeImporter < ActiveImporter::Base
imports Employee, detect_columns: true
column 'Department', :department do |name|
Department.find_by(name: name)
end
end
Notice the option hash on the imports clause.
I'll try to find some time to work on this, or if you want to pitch in with a pull request, go for it. However we need to address the problem I mentioned in the commit's comments before releasing any of this.
Thanks for your interest and your support.