-
-
Save ratnikov/610418 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create users from csv file | |
| def create_students_from_csv | |
| @csv_file = CSV::Reader.parse(params[:csv_file]) | |
| # Did we get a file? | |
| if @csv_file | |
| # Remove headings | |
| @csv_file.shift | |
| count_saved = @csv_file.inject(0) do |acc, row| | |
| a = Account.new(:username => row[2], :email => row[2], :real_name => row[0], :account_type => 'student') | |
| a.password = rand(2**256).to_s(36)[0..7] | |
| a.active = true | |
| a.save ? acc + 1 : acc | |
| end | |
| flash[:notice] = "CSV Import Successful, #{count_Saved} new students added to the database" | |
| redirect_to portals_path | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment