Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add seed data to development database for testing

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?

like image 627
E.E.33 Avatar asked Dec 16 '22 08:12

E.E.33


1 Answers

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
like image 144
Gazler Avatar answered Dec 27 '22 04:12

Gazler