What is the best way to seed a database in Rails?

I have a rake task that populates some source data in my rails application. For example, countries, states, mobile operators, etc.

As I configured it now, I have a bunch of creating statements in files in / db / fixtures and a rake task that handles them. For example, one model has themes. I have a theme.rb file in / db / fixtures that looks like this:

Theme.delete_all Theme.create(:id => 1, :name=>'Lite', :background_color=>'0xC7FFD5', :title_text_color=>'0x222222', :component_theme_color=>'0x001277', :carrier_select_color=>'0x7683FF', :label_text_color=>'0x000000', :join_upper_gradient=>'0x6FAEFF', :join_lower_gradient=>'0x000000', :join_text_color=>'0xFFFFFF', :cancel_link_color=>'0x001277', :border_color=>'0x888888', :carrier_text_color=>'0x000000', :public => true) Theme.create(:id => 2, :name=>'Metallic', :background_color=>'0x000000', :title_text_color=>'0x7299FF', :component_theme_color=>'0xDBF2FF', :carrier_select_color=>'0x000000', :label_text_color=>'0xDBF2FF', :join_upper_gradient=>'0x2B25FF', :join_lower_gradient=>'0xBEFFAC', :join_text_color=>'0x000000', :cancel_link_color=>'0xFF7C12', :border_color=>'0x000000', :carrier_text_color=>'0x000000', :public => true) Theme.create(:id => 3, :name=>'Blues', :background_color=>'0x0060EC', :title_text_color=>'0x000374', :component_theme_color=>'0x000374', :carrier_select_color=>'0x4357FF', :label_text_color=>'0x000000', :join_upper_gradient=>'0x4357FF', :join_lower_gradient=>'0xffffff', :join_text_color=>'0x000000', :cancel_link_color=>'0xffffff', :border_color=>'0x666666', :carrier_text_color=>'0x000000', :public => true) puts "Success: Theme data loaded" 

The idea here is that I want to install some themes for users to get started. I have a problem with this method.

Setting the identifier does not work. This means that if I decide to add a theme, let her call it "Red", then I would just like to add a theme operator to this binding file and call the rake task to reload the database. If I do this because the topics are related to other objects and their identifier changes during this reinitialization, all links are broken.

My question is, first of all, is this a good way to handle seeding a database? In a previous post, this was recommended to me.

If so, how can I hardcode the identifiers and are there any flaws?

If not, what is the best way to plant a database?

I will truly appreciate the long and thoughtful answers that include best practices.

+67
ruby database ruby-on-rails task seed
Apr 17 '09 at 16:24
source share
8 answers

Update as these answers are a bit outdated (although some of them are still applicable).

Simple function added in rails 2.3.4, db / seeds.rb

Provides a new rake command

 rake db:seed 

Good for filling out regular static entries like states, countries, etc.

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

* Please note that you can use the instruments if you have already created them to also complete the db: seed task by placing the following in the 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 the Fixtures constant

 require 'active_record/fixtures' ActiveRecord::Fixtures.create_fixtures("#{Rails.root}/test/fixtures", "fixtures_file_name") 
+96
Jan 15 '10 at
source share

factory_girl sounds like it will do what you are trying to achieve. You can define all common attributes in the default definition and then override them at creation time. You can also pass the factory identifier:

 Factory.define :theme do |t| t.background_color '0x000000' t.title_text_color '0x000000', t.component_theme_color '0x000000' t.carrier_select_color '0x000000' t.label_text_color '0x000000', t.join_upper_gradient '0x000000' t.join_lower_gradient '0x000000' t.join_text_color '0x000000', t.cancel_link_color '0x000000' t.border_color '0x000000' t.carrier_text_color '0x000000' t.public true end Factory(:theme, :id => 1, :name => "Lite", :background_color => '0xC7FFD5') Factory(:theme, :id => 2, :name => "Metallic", :background_color => '0xC7FFD5') Factory(:theme, :id => 3, :name => "Blues", :background_color => '0x0060EC') 

When used with faker, it can quickly populate the database with associations without having to talk to Fixtures (yuck).

I have code like this in a rake task.

 100.times do Factory(:company, :address => Factory(:address), :employees => [Factory(:employee)]) end 
+25
Apr 17 '09 at 19:33
source share

Typically 2 types of seed data are required.

  • Key data that the core of your application can rely on. I call it ordinary seed.
  • Environmental data , for example, for application development, it is useful to have a bunch of data in a known state that we can use to work in the application locally (Factory Girl's answer above covers such data).

In my experience, I have always come across the need for these two types of data. Therefore, I collected a small stone that extends Rails samples and allows you to add several common seed files under db / seed / and any environmental seed data under db / seed / ENV, for example db / seed / development.

I found that such an approach is enough to give my seed data some structure and gives me the opportunity to set up my development environment or intermediate stage in a known state by simply doing:

 rake db:setup 

The luminaires are fragile and opaque to maintain, like ordinary sql dumps.

+24
Apr 04 2018-11-11T00:
source share

Using seeds.rb or FactoryGirl fine, but they are great for fixed data structures and testing.

seedbank gem can give you more control and modularity to your seeds. It inserts rake tasks, and you can also define the dependencies between your seeds. Your rake task list will have these additions (for example.):

 rake db:seed # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env. rake db:seed:bar # Load the seed data from db/seeds/bar.seeds.rb rake db:seed:common # Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb. rake db:seed:development # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/development/*.seeds.rb. rake db:seed:development:users # Load the seed data from db/seeds/development/users.seeds.rb rake db:seed:foo # Load the seed data from db/seeds/foo.seeds.rb rake db:seed:original # Load the seed data from db/seeds.rb 
+21
Nov 26 '13 at 7:54
source share

Instead of using explicit creations, use YAML files. Using simple syntax, you can fill in all the values โ€‹โ€‹of an object. In fact, if you know anything about rail testing, this is the standard way to plant a test database. Check out these pages:
http://railspikes.com/2008/2/1/loading-seed-data http://quotedprintable.com/2007/11/16/seed-data-in-rails

+2
Apr 17 '09 at 16:50
source share

Add it to the database migration, so everyone gets it as it updates. Manage all your logic in ruby โ€‹โ€‹/ rails code, so you never have to bother with explicit identifier settings.

0
Apr 17 '09 at 19:13
source share

Rails has a built-in way to generate data, as described here .

Another way is to use the gem for a more advanced or simple sowing, for example: seedbank .

The main advantage of this gemstone and the reason I use it is that it has advanced features, such as data load dependencies and seed environment data.

Adding an up-to-date answer as this answer was the first in google.

0
May 05 '14 at 14:21
source share

The best way is to use lights.

Note. Keep in mind that fixtures make direct inserts and do not use your model, so if you have callbacks that fill data, you need to find a workaround.

-four
Apr 17 '09 at 19:10
source share



All Articles