Best way since Rails 6 is to use upsert_all
/insert_all
.
posts = []
10000.times do |iter|
# construct a hash of our Post values
posts << { title: Faker::Company.name, body: Faker::Company.bs }
end
# create all our Posts with a single INSERT
Post.upsert_all posts
puts "finished seeding the database"
This guy did some research on this: https://railsnotes.xyz/blog/seed-your-database-with-the-faker-gem