Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seed Devise users efficiently?

I'm trying to seed about 100,000 users using rake db:seed in my Rails 3 project and it is really slow!

Here's the code sample:

# ...
User.create!(
  :display_name => "#{title} #{name} #{surname}",
  :email => "#{name}.#{surname}_#{num}@localtinkers.com",
  :password => '12341234'
)

It works, but it is really slow because for each user:

  1. Devise issues a SELECT statement to find out if the email is already taken.
  2. A separate INSERT statement is issued.

For other objects I use "ar-extensions" and "activerecord-import" gems as follows:

tags.each do |tag|
  all_tags << Tag.new(:name => tag)
end

Tag.import(all_tags, :validate => false, :ignore => true)

The above creates just one INSERT statement for all the tags and it works really fast, just like MySql database restore from the SQL dump.

But for users I cannot do this because I need Devise to generate encrypted password, salt, etc for each user. Is there a way to generate them on the SQL side or are there other efficient ways of seeding users?

Thank you.

like image 950
Alex Kovshovik Avatar asked Mar 25 '11 14:03

Alex Kovshovik


1 Answers

How about:

u = User.new(
  :display_name => "#{title} #{name} #{surname}",
  :email => "#{name}.#{surname}_#{num}@localtinkers.com",
  :password => '12341234'
)
u.save!(:validate => false)

This should create and save the record without executing the validations, and therefore without checking for e-mail address uniqueness. Obviously the downside of this is that you're not being protected by any other validations on the user too, so make sure you check your data first!

like image 117
Paul Russell Avatar answered Sep 28 '22 02:09

Paul Russell