2

I'm developing a Rails application and within that application I developed a Rake task that will read entries from a file and store them into the DB. Producing the code was no problem, but I'd like to know, where do I place the file that is read? Is there a convention for that, if yes, what is it?

I know I could have used the seed.rb file but is it ok, by the standards, to load and read a file from there?

Thanks in advance!

4 Answers 4

3

Yes, put the data you wish to load in the db/seeds.rb file and to load it run rake db:seed. This is what this file was designed to do.

3
  • +1 I didn't even know about this task. The problem is that there's no migration/versioning for seed, is there? Apr 7, 2010 at 23:54
  • Do you mean reading the file from seeds.rb? If that's what you meant, what to do when there are many files to be read?
    – Guilherme
    Apr 8, 2010 at 2:10
  • 1
    @Guilhereme Any ruby code can be in seeds.rb, so it can refer to other files. Make a directory under db called seed, perhaps? Apr 8, 2010 at 8:03
2

I don't think there's a hard and fast Rails convention for this case. When it comes to seed data I put mine in a subfolder of db.

0

The yaml_db plugin dumps/loads content from/to the database from the file rails_root/db/data.yml i'm not sure if this is by convention, however, the content is db related making the db folder an appropriate choice

0

Where to put stuff in Rails is a problem that I've been working around for a while. The question is whether your file can fit into one of the existing concerns. For instance, is it configuration information?

Anyway, perhaps a similar fit would be the schema.rb, which is put in the db directory. Do not modify the schema.rb with your data, of course: I'm merely suggesting that the db directory, or a subdirectory, might be a place to put your file(s).

On the other hand, if you don't see any directories that hold anything similar -- it's not one of the main categories in app, nor any of the main categories above that -- then you can just make up a name and use that.

2
  • Never, EVER edit db/schema.rb! It will be updated every time you run a migration.
    – Ryan Bigg
    Apr 7, 2010 at 20:11
  • When you delete comments these conversations become surreal. Apr 7, 2010 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.