2017-03-03 27 views
0

我想运行一个简单的循环来创建多个种子用户:用色器件用户播种DB失败:ActiveRecord的:: RecordInvalid:验证失败:电子邮件无效

在seeds.rb:

5.times do 
    user = User.new(
     email: RandomData.random_space_email, 
     password: 'teatea', 
     password_confirmation: 'teatea' 
    ) 
    user.skip_confirmation! 
    user.save! 
end 

RandomData.random_space_email:

def self.random_space_email 
    "#{@@names.sample}@#{@@space_words.sample}.#{@@space_words.sample}" 
end 

当我运行rake db:reset我得到这个错误:ActiveRecord::RecordInvalid: Validation failed: Email is invalid

但是,如果我将循环设置为仅运行一次(1.times do),则所有操作都按预期运行。

任何帮助将是美好的!

+0

尝试使用'email =(0..8).map {('a'..'z')。to_a [rand(26)]}。加入+“@ yopmail.com”查看一般电子邮件地址。 –

+0

@MayurShah有趣!谢谢! – mtaff

+0

不客气。 –

回答

0

仔细检查后:

def self.random_space_email 
    "#{@@names.sample}@#{@@space_words.sample}.#{@@space_words.sample}" 
end 

我发现@@space_words有时包含空格的字符串...这似乎导致devise抛出一个错误,因为没有电子邮件地址中的空格可以。

相关问题