Question

I've been looking for a week now. I'm using Rails 3.

I have a document section in my app and I'd like to populate the database with fake data. So far it was super easy: just add a .yml file in the Fixture folder and that's it.

Now what I'd like is to have a fixture file (.yml) that would populate the DB with documents when I rake db:fixtures:load. I just don't know how to achieve this. I've been looking to the fixture_file_upload function but I don't think it's the way to go.

Any idea? Thanks in advance.

Edit: to be perfectly clear, I'd like to upload a document that I list in my Document fixture. So when I rake db:fixtures:load, the document is actually uploaded.

Was it helpful?

Solution

Normally loading seed data is done using rake db:seed which just executes your db/seed.rb. There you can do anything you want.

If you want to reuse your fixtures you can just load them manually:

require 'active_record/fixtures'
ActiveRecord::Fixtures.create_fixtures(Rails.root.join('test/fixtures'), 
                                       'your_yml_file')

OTHER TIPS

Pass the FIXTURES Environment Variable

Assuming that you have test/fixtures/documents.yml and that you don't mind clobbering the data currently in the documents table, you can load your fixture with:

rake db:fixtures:load FIXTURES=documents

If desired, you can also pass the appropriate RAILS_ENV to load your data into something other than your development database, such as test or production. Make sure you back up your database first, though. A typo could wipe out your current data set.

Are you installing fixture data for tests, or using test/fixtures to load fake development data? If the former is the case, it's hard because you don't want the test runner to be uploading a bunch of files between every test. If the latter is the case, then you ought to use rake db:seeds instead. That runs db/seeds.rb in the context of your Rails app. Use an environment flag to load the fake data in development only. Then you can use the API of your attachment library to "upload" the files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top