Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import Devise users #16

Closed
joshuaswilcox opened this issue Aug 16, 2014 · 15 comments
Closed

Can't import Devise users #16

joshuaswilcox opened this issue Aug 16, 2014 · 15 comments

Comments

@joshuaswilcox
Copy link

I can't seem to import users that are created through Devise. I get an error
Missing column for value at index 3
which makes sense since "password" isn't an actual field its "encrypted_password", but I am providing a default password for these users that I am importing which they are required to change after the import. Is there anyway around this?

@Fivell
Copy link
Member

Fivell commented Aug 16, 2014

@joshuaswilcox , can you show example of imported file, and resource #active_admin_import declaration?

@joshuaswilcox
Copy link
Author

CSV

Billy,Corgan,billy@corgan.com,123456789,123456789,1,1,male

and declaration

active_admin_import :validate => false,
                        :template_object => ActiveAdminImport::Model.new(
                            :hint => "file will be imported with such header format: 'first_name','last_name','email','password', 'password_confirmation', 'team_id', 'age', 'gender'",
                            :csv_headers => ["first_name","last_name","email","password","password_confirmation","team_id","age","gender"]
                        ),
                        :batch_size => 1000

I was thinking I can probably call a before_import proc to encrypt the password before import

@Fivell
Copy link
Member

Fivell commented Aug 16, 2014

@joshuaswilcox , unfortunately I can't check it right now, because I have limited internet access, the one thing you can try is to use before_batch_import hook like in readme example, in this proc you should have access to import.csv_lines (lines to import) so you can try to change them in loop.

@joshuaswilcox
Copy link
Author

@Fivell, thanks that was I was thinking. I'll report back

@Fivell
Copy link
Member

Fivell commented Aug 16, 2014

Also if you use default password, you can move it out of the file, and add it dynamically , I mean you can append value to each csv_line and also to csv_headers in before_batch_import.

@joshuaswilcox
Copy link
Author

Ah yes that makes sense as well. So I called:

import.csv_lines[0][3] = User.new(:password => import.csv_lines[0][3]).encrypted_password

in the before_batch_import and it properly set the value I was looking for, but I get:

(PG::NotNullViolation: ERROR:  null value in column "created_at" violates not-null constraint

but my db definition just uses the normal t.timestamps. Any ideas?

@Fivell
Copy link
Member

Fivell commented Aug 16, 2014

@joshuaswilcox, try to set timestamps option.

active_admin_import :validate => false, :timestamps=> true, .....

@Fivell
Copy link
Member

Fivell commented Aug 17, 2014

@joshuaswilcox , so does it finally work ?

@joshuaswilcox
Copy link
Author

Yes, brilliant, thanks for the help!

@andymilk
Copy link

@joshuaswilcox do you have the full import block for this? I'm having a similar problem, but getting this error: Error: Missing column for value at index 2

@joshuaswilcox
Copy link
Author

sure

  active_admin_import :validate => false,
                            :before_batch_import => proc { |import|
                              import.csv_lines[0][3] = User.new(:password => import.csv_lines[0][3]).encrypted_password
                            },
                            :template_object => ActiveAdminImport::Model.new(
                                :hint => "file will be imported with such header format: 'first_name','last_name','email','password', 'team_id', 'age', 'gender'"
                            ),
                            :timestamps=> true,
                            :batch_size => 1000

@asecondwill
Copy link

I had a bit of trouble with this till the penny dropped to change the header to encrypted_password.

so

first_name,last_name,email,encrypted_password
joe,blogs,user@example.com,12345678


  active_admin_import :validate => false,
                            :before_batch_import => proc { |import|
                             # binding.pry                              
                              import.csv_lines[0][3] = User.new(:password => import.csv_lines[0][3]).encrypted_password
                            },
                            after_batch_import: ->(importer) {
                             #the same
                            # binding.pry
                            },
                            :template_object => ActiveAdminImport::Model.new(
                                :hint => "file will be imported with the header format: 'first_name','last_name','email','password'"
                            ),
                            :timestamps=> true,
                            :batch_size => 1000 

@kyreeves
Copy link

@asecondwill @joshuaswilcox

How would you do this if you had multiple attributes that were encrypted? I can't seem to figure it out.

@kyreeves
Copy link

@Fivell do you have any suggestions?

@Fivell
Copy link
Member

Fivell commented Oct 15, 2015

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

No branches or pull requests

5 participants