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!
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.
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
.
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
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.
精彩评论