Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created March 14, 2013 16:13
Show Gist options
  • Select an option

  • Save wrburgess/5162702 to your computer and use it in GitHub Desktop.

Select an option

Save wrburgess/5162702 to your computer and use it in GitHub Desktop.
Rails seed database with rake task
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