简体   繁体   中英

Rails random records generator

I ask myself if there is a gem or a method to generate a large amount of records with random data (which are valid for the model)

The goal is to test my application with a lot of data in the database.

I didn't find anything on the web, but i don't have use right keywords.

Do you know something like that?

I find the faker gem quite useful. You'll have to write your record creation code yourself, though.

You can use Faker gem. And, following is sample code to create 10 user records. You can create as many records as you want.

10.times do  
  user = User.new
  user.first_name = Faker::Name.first_name
  user.last_name = Faker::Name.last_name
  user.email = Faker::Internet.email
  user.phone = Faker::PhoneNumber.cell_phone
  user.save
end

Few more gems for fake data

Fabrication - A simple and powerful object generation library.

factory_bot - A library for setting up Ruby objects as test data.

Fake Person - Uses some of the most popular given & surnames in the US & UK.

faker - A library for generating fake data such as names, addresses, and phone numbers.

ffaker - A faster Faker, generates dummy data, rewrite of faker.

Forgery - Easy and customizable generation of forged data

Machinist - Fixtures aren't fun. Machinist is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM