There is a convenient way to populate tables - db / seed.rb. Just add a script to create users in it and execute:
rake db:seed
Below you can see an example of the User model with the email and username fields:
# Inserting default security users users = { admin: { username: 'admin', email: 'admin@gmail.com', password: 'adminpass', password_confirmation: 'adminpass', is_admin: true }, administrator: { username: 'administrator', email: 'administrator@gmail.com', password: 'administrator', password_confirmation: 'administrator', is_admin: true } } users.each do |user, data| user = User.new(data) unless User.where(email: user.email).exists? user.save! end end
Please note that validation methods apply here.
Here you can find more examples of using the seed.rb file and here it is rayn rails cast.
gotqn Jul 03 '14 at 21:20 2014-07-03 21:20
source share