Created
March 14, 2013 16:13
-
-
Save wrburgess/5162702 to your computer and use it in GitHub Desktop.
Rails seed database with rake task
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
| namespace :project_name do | |
| require 'csv' | |
| desc 'Migrate previous user database' | |
| task :migrate_users => [:environment] do | |
| input = '' | |
| STDOUT.puts 'Seed with previous users?' | |
| input = STDIN.gets.chomp | |
| if input == 'y' | |
| puts 'Executing tasks...' | |
| CSV.foreach("#{Rails.root}/db/fill/previous_users.csv", :headers => :first_row) do |row| | |
| User.create!({ | |
| password: (0...8).map{(65+rand(26)).chr}.join, | |
| email: row[0], | |
| salutation: row[1], | |
| first_name: row[2], | |
| last_name: row[3] | |
| }, without_protection: true | |
| ) | |
| puts 'User created' | |
| end | |
| else | |
| puts 'Aborting tasks...' | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment