Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Populate information without to specify all the column names #28

Open
heridev opened this issue Sep 25, 2014 · 2 comments
Open

Populate information without to specify all the column names #28

heridev opened this issue Sep 25, 2014 · 2 comments

Comments

@heridev
Copy link

heridev commented Sep 25, 2014

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 :)

@grillermo
Copy link

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.
grillermo@7070c02

@gnapse
Copy link
Contributor

gnapse commented Sep 29, 2014

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants