#179 Seed Data
Sep 14, 2009 | 7 minutes | Active Record, Rails 2.3
Rails 2.3.4 includes a conventional way to add seed data to your application - no more including it in the migration files.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Thanks Ryan.
By the way for those who use firefox you can increase the site width for this site using a style I created a while ago.
You can find it here.
http://userstyles.org/styles/15366
great screencast as always.
btw, what plugin do you use to nicely display db-records in console as a table?
Thank you very much!
Another great one. Thank you.
@rwz he is using Hirb http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html
@elad tnx
O(∩_∩)O~good!
Thanks, Ryan!
It's indeed a very simple addition to rails, but it provides a good convention. I used to do this initial data loading on a custom rake task, normally called something like rake project:install, which would migrate the database and load initial data, including an admin user as well.
Thanks Ryan for another great episode. I think I can use it in my project. I am working on migrating legacy data from my postgres database to SQL server database with some added columns and schema changes. Can anyone tell me how can I accomplish this with some custom rake task? Can I use the concept in this episode to accomplish this. If yes, how do I read data from another database and insert it to my new database with some modification if needed?
My idea here is one this rake task is written, I can just run that rake task during deployment and get the data populated in the new database from the QA database.
I will appreciate your good suggestions.
What's the benefit of using the built in vs seed_fu?
What are your recommendations for sequential migrations that require seed data from previous migrations?
This generally only applies to legacy databases. I might need to move users from a legacy table to a new table and relate them to yet-to-be-seeded data (such as a default group).
Is it possible to have multiple seed files? And just reference them in the migration?
I love the new additions to rails, both 2.3 and 3.0; however this is NOT one of my favorites. I feel this feature was thrown in without a lot of thought about its uses. If you are doing incremental design and adding new features only as you need them, then this feature is outright broken.
For example, say I have a migration that creates an "issues" table and that table has a link to a status table on it. I use seeds.rb to insert "Open", "Closed" etc into the status table. Later I decided I need to add priority to issues and I add a priority table... wait how can I seed this table, seeds.rb has already been run and I don't want to reseed my status table, I may have user values in there now.
The only way I really see this working would be to add a seeds directory next to the migrations directory and create a seed file for each migration that gets run after the migration. Otherwise we will have to keep adding any seeds past the first into the migrations.
Good job, though I agree with rjordan about the usefulness of this particular feature. Also, perhaps it's time for captcha here? I don't know why, but it seems alot of rails blogs are cluttered with spam comments.
On the subject of seed data, I was wondering what is the best approach for accessing records in code. At present I use, for example...
def self.Pending() find(1); end
...inside my seeded models like OrderStatus. Then I can test using order.status == OrderStatus.Pending. The problem is that these class methods are evaluated when the environment loads, which is necessary to load seed data in the first place. It would also be nice to be able to cache these objects for the life of the application, as they are only referring to static data after all. Is there a more appropriate convention for this which maintains the readability of the code I have? Thanks!
Perfect timing, I has just been implementing fixtures for testing using another method, but obviously this is just great.
Thanks Ryan!
I think the countries seed should use
Country.find_or_create_by_name_and_code(name, code)
so that you don't delete the countries that are referenced by other tables (if you chose to do that)
nice work
So if you call Country.delete_all won't the break all previous associations! Unless your index is based on country code which should be noted in the migration creation.
OK, so with seed-fu, rake db:seed RAILS_ENV=test would seed the test database. With the builtin method, it doesn't. How do I get seed data into the test environment?
(Yes, it needs to be there — there's a validates_existence_of that depends on the seed data being loaded…)
Great railscast as usual.
Just one suggestion. For lists of countries and states, the Carmen gem is way better than copy-and-pasting:
http://github.com/jim/carmen
You also get some handy helpers to convert between full name and abbreviation.
Ryan Boa Tarde!
Estou usando o código a seguir para popular algumas tabelas:
http://pastie.org/650693
Ocorre que se executar mais de uma vez, a sequence do postgres continua. Como poderia fazer para resetar a sequence sempre que rodar rake db:seed ???
Abraços
Thank you for its - much help in decision of my problems
Use rake
db:seed SEED='file_matching_ pattern'
for specific file to populate the database.
Enjoy and do not again repeatedly update all the seed_fu dependent tables again and again
Hi ryan.
I've created a plugin to seed data acording to the environment being loaded.
I've been using it to seed testing, staging and production databases with different kinds of data without having a big seeds.rb.
Check it out!
http://github.com/franciscotufro/environmental_seeder
Wow Ryan, that's a lot of spam you've managed to harvest. :-) Perhaps a CAPTCHA of some sort might be in order?
I hope my clicking those links for you at least puts them in a queue so you can "delete all".
eat website,have very interested articles ,i have visit 'em
What would the code look like to read the codes.csv file in db/seedData directory? It has three columns. name, date, location
Thank you!!
I knocked up seedbank to give your seeds a little structure like this;
git://gist.github.com/900969.git
https://github.com/james2m/seedbank
seeds.rb has a major flaw. If my app "needs" the data to function, then my tests fail without it. The test environment automatically flushes this seed data. Sure, I could do everything twice and put the same data back in once the test environment loads, but I shouldn't have to. There has to be a way to tell rails to load my seeds after purging in preparation for testing rather than before. (It would be better if somehow rails would only purge the non-seed data, but now I'm just dreaming.)
Hi Ryan. Having an issue with seeding and I wondered if you could shed some light. I am seeding my database (with no issues) other than the before filters are not called when seeding. Is there anyway of doing the rake db:migrate call and ensuring before filters are called?
Hi Ryan,
I just came up with the following code to help ensure that you get consistent ID's when using the delete and repopulate seed approach.
Load seeds into test db automatically:
http://stackoverflow.com/questions/1574797/how-to-load-dbseed-data-into-test-database-automatically
Hello,
I implement Fixtures as you. But when I run rake db:seed, I meet error:
rake aborted!
uninitialized constant Fixtures
My seed.rb is:
require 'active_record/fixtures'
Fixtures.create_fixtures("#{Rails.root}/db", "categories")
My categories.yml is in db directory:
one:
name: Flying
I search on google and don't find anything. Please help me, thank you!
Hey banhbaochay,
You should use the following instead.
ActiveRecord::Fixtures.create_fixtures("#{Rails.root}/db", "categories")
Stack Overflow
Thank you very much), I am impressed)
When I upgraded to Rails 3.2 I had issues with running the seed tasks, only in production. When the seed file ran, it would not find any ActiveRecord models; but would work fine when called from the console.
Turns out, threadsafe was enabled in production, and rake tasks would get tripped up. To fix I had to do:
also a good thing for capistrano recipes.