Skip to content

Instantly share code, notes, and snippets.

Created October 4, 2010 20:54
Show Gist options
  • Select an option

  • Save anonymous/610413 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/610413 to your computer and use it in GitHub Desktop.
# 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
c = 0
# Remove headings
@csv_file.shift
# Loop through our records
@csv_file.each do |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
if a.save
# Add 1 to our counter, will be used for displaying ammount of saved users
c += 1
end
end
flash[:notice] = "CSV Import Successful, #{c} 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