I have a few tables that I would like to add about 10 rows of data to, in a manner that respects and illustrates their relationships.
How can I add seed data (dummy data) to my applications' development database for testing?
I'm hoping someone could point me to a rails friendly method for doing this.
Is there an easy way to make the CRUD methods in each table perspective controllers?
This is what the db/seeds.rb
file is for.
You can execute it with rake db:seed
The default contents of seeds.rb
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Mayor.create(:name => 'Daley', :city => cities.first)
You can use ruby in this, so to insert 10 users:
1.upto(10) do |i|
User.create(:name => "User #{i}")
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With