简体   繁体   English

Seeds.rb需要使用随机字符串作为种子

[英]Seeds.rb needs to be seeded with random string

I would like to seed my seeds.rb file with a set of random strings. 我想用一组随机字符串为seed.rb文件添加种子。

I currently have a "name" category a "about_me" category such as: 我目前有一个“名称”类别和“ about_me”类别,例如:

  Name: John, Jim, Joe, Jamie, Kim, Monica, Erica, Nicole
  About_me: "I am blonde", "I am a brunette", "I have red hair", "I work at the museum"

I would like to seed 200 users with random sets of names & about_me's. 我想为200个用户提供随机的名称和about_me。 How would i get around doing this? 我该如何解决? Can someone point me in the right direction? 有人可以指出我正确的方向吗?

I currently have: 我目前有:

 200.times do |i|
   User.create(rand(name: i, about_me: i))

That is not even close to working for me, so I was wondering how i can tackle this problem. 那甚至还不能为我工作,所以我想知道如何解决这个问题。 Thank you in advance! 先感谢您!

If you want the name and about_me to be completely random (ie make no sense at all), you can just generate random strings as shown in How to generate a random string in Ruby . 如果您希望nameabout_me完全随机(即完全没有意义),则可以生成随机字符串,如如何在Ruby中生成随机字符串所示。 However, if you want your information make some sense, you can create a list of possible names and about me strings and choose from random. 但是,如果您希望您的信息有意义,则可以创建可能的名称以及about me字符串的list ,然后从中选择。

How about something like not: 怎么样呢?

200.times do |i|
  User.create(:name => SecureRandom.hex(12), :about_me => SecureRandom.hex(12))

If you don't care about the generated strings making sense at all. 如果您根本不关心生成的字符串,那么就完全有意义了。 If you do need the strings to be "valid" then you will need to build a file or an array of names and pick random elements from it. 如果确实需要字符串“有效”,则需要构建文件或名称数组并从中选择随机元素。

You can also look into the Faker gem which is used to generate fake data: 您还可以查看用于生成伪造数据的Faker gem:

https://github.com/stympy/faker https://github.com/stympy/faker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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