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!

Guilherme
  • 551
  • 4
  • 16

4 Answers4

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.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • +1 I didn't even know about this task. The problem is that there's no migration/versioning for seed, is there? – Dan Rosenstark Apr 07 '10 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 08 '10 at 02: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? – Dan Rosenstark Apr 08 '10 at 08: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.

Chris
  • 1,826
  • 12
  • 15
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.

Community
  • 1
  • 1
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
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

seanbehan
  • 1,463
  • 15
  • 23