如何高效地培育 Devise 用户?
我试图在我的 Rails 3 项目中使用 rake db:seed 为大约 100,000 个用户提供种子,但速度非常慢!
这是代码示例:
# ...
User.create!(
:display_name => "#{title} #{name} #{surname}",
:email => "#{name}.#{surname}_#{num}@localtinkers.com",
:password => '12341234'
)
它可以工作,但非常慢,因为对于每个用户:
- Devise 发出一条 SELECT 语句来查明电子邮件是否已被占用。
- 发出单独的 INSERT 语句。
对于其他对象,我使用“ar-extensions”和“activerecord-import”gems,如下所示:
tags.each do |tag|
all_tags << Tag.new(:name => tag)
end
Tag.import(all_tags, :validate => false, :ignore => true)
上面只为所有标签创建了一个 INSERT 语句,并且它可以工作 非常快,就像从 SQL 转储恢复 MySql 数据库一样。
但对于用户我不能这样做,因为我需要 Devise 为每个用户生成加密的密码、盐等。有没有办法在 SQL 端生成它们,或者有其他有效的方法来播种用户?
谢谢。
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:
- Devise issues a SELECT statement to find out if the email is already taken.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
这应该创建并保存记录而不执行验证,因此不检查电子邮件地址的唯一性。显然,这样做的缺点是您也不受用户的任何其他验证的保护,因此请确保首先检查您的数据!
How about:
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!