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

Add roles after importing users #159

Closed
sharathm opened this issue Aug 23, 2018 · 4 comments
Closed

Add roles after importing users #159

sharathm opened this issue Aug 23, 2018 · 4 comments
Labels

Comments

@sharathm
Copy link

sharathm commented Aug 23, 2018

Hi,
I have a standard Devise User model, for which I want to provide a bulk upload feature.
I also have rolify integrated and so as people register on the site, I give options for them to either register as a student or a teacher.
What I want to do, is use active_admin_import to provide a bulk upload feature for teachers only. So basically after the import is done, i need to assign them to teacher role.
In a normal scenario, this is done by new_user.add_role(:teacher), assuming new_user is a newly created user object.
I am not able to figure out how to get access to the instance of the created object after insertion so that I can accomplish the same.

ActiveAdmin.register User do

active_admin_import validate:false,
after_batch_import: proc { |import|

**<not sure what to put here>**

},
before_batch_import: proc { |import|
	import.csv_lines.length.times do |i|
		import.csv_lines[i][2] = User.new(password: import.csv_lines[i][2]).encrypted_password
	end
},
template_object: ActiveAdminImport::Model.new(
	hint: "file will be imported with such header format: 'full_name',''email','password'. They will assigned as teachers"
),
timestamps: true,
batch_size: 1000

index do
    selectable_column
    column :full_name
    column :email
	column :phone_number
	column :roles do |user|
		user.roles.collect {|c| c.name.capitalize }.to_sentence
	end
	column :current_sign_in_at
	column :last_sign_in_at
	column :created_at

  end
end

Is my use of after_batch_import correct or should I go for after_import and in each case, how do i get access to the instance of the newly imported object or the array of objects imported so that I can assign the role.

Any help is appreciated

Thank you

@Fivell
Copy link
Member

Fivell commented Jan 15, 2019

@sharathm I'm not familiar with rolify, can u share database structure ?

@sharathm
Copy link
Author

@Fivell - Here you go. https://github.com/RolifyCommunity/rolify

@Fivell
Copy link
Member

Fivell commented Jan 18, 2019

@sharathm please show structure db of your table u want to import

@Fivell
Copy link
Member

Fivell commented May 6, 2019

duplicate of #66

@Fivell Fivell closed this as completed May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants