jeudi 31 décembre 2015

How to populate empty attributes with random values from seeds.rb

I've generated User model with attributes: first_name, last_name, city. Then, I've created 200 instances of the class using seed.rb and gem 'faker'. After all, I have added one more attribute to this model - age:string, so now in every instance of the class the age = 'nil'. Now, I want to populate every single user with randomly generated number from range 10..99. Here is my code from seed.rb:

users = User.all
users.each do |element|
    element.age = rand(10..99).to_s
end

When I type rake db:seed it seems to be successfull but when I check the database, each age:string is still 'nil'. Do you have any idea what might have gone wrong?

Aucun commentaire:

Enregistrer un commentaire