views:

69

answers:

4

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!

A: 

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.

Yar
Never, EVER edit db/schema.rb! It will be updated every time you run a migration.
Ryan Bigg
Which downvote?
Ryan Bigg
When you delete comments these conversations become surreal.
Yar
+2  A: 

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.

Chris
A: 

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

bseanvt
+1  A: 

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.

Ryan Bigg
+1 I didn't even know about this task. The problem is that there's no migration/versioning for seed, is there?
Yar
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
@Guilhereme Any ruby code can be in seeds.rb, so it can refer to other files. Make a directory under db called seed, perhaps?
Yar