How can I stop the fake game from failing to verify the uniqueness of a ruby ​​username on rails?

Using faker to populate my database with fake users, and I have a validation rule that ensures that usernames are unique and cannot be registered more than once.

When I run rake db: filling it never reaches 1000, it stops someday before it gets there, and because I use create! he shows me what I need to see that: Usernname is already registered.

My question is, how can I add a number to the username and every time it comes back around a number, it increases? Thus, no username will be the same.

eg.

John1 pete2 sally3 smith4 luke5 john6 sally7

etc.

or is there another way to make sure that no usernames appear more than once?

namespace :db do namespace :development do desc "Create user records in the development database." task :populate => :environment do require 'faker' 1000.times do User.create!( :username => Faker::Internet.user_name, :email => Faker::Internet.email, :password => "greatpasswordhuh" ) end end end end 

Yours faithfully

+4
source share
2 answers

It worked for me. It is not entirely clear why "n" creates numbers.

  namespace :db do desc "Create user records in the development database." task :populate => :environment do require 'faker' 100.times do |n| username = "#{Faker::Name.first_name}#{n}" User.create!( :username => username, :email => Faker::Internet.email, :password => "greatpasswordhuh" ) end end end 
+2
source

You can add an index to your loop.

 1000.times do |n| username = Faker::Internet.user_name username = "#{username}_#{n}" ... end 
+10
source

Source: https://habr.com/ru/post/1392394/


All Articles