"what is the best way to seed a database in rails?" Code Answer

1

updating since these answers are slightly outdated (although some still apply).

simple feature added in rails 2.3.4, db/seeds.rb

provides a new rake task

rake db:seed

good for populating common static records like states, countries, etc...

http://railscasts.com/episodes/179-seed-data

*note that you can use fixtures if you had already created them to also populate with the db:seed task by putting the following in your seeds.rb file (from the railscast episode):

require 'active_record/fixtures'
fixtures.create_fixtures("#{rails.root}/test/fixtures", "operating_systems")

for rails 3.x use 'activerecord::fixtures' instead of 'fixtures' constant

require 'active_record/fixtures'
activerecord::fixtures.create_fixtures("#{rails.root}/test/fixtures", "fixtures_file_name")
By antdwash on May 13 2022

Answers related to “what is the best way to seed a database in rails?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.