Where's the correct place to put files to parse/add to a database?

Hi,

If I have some files, say FOOD_DESCRIPTIONS and FOOD_GROUPS that I need to parse and add their values to a database, where's the best place to put them if I am doing it via migrations?

Is it bad to put them in the db/migrate folder along with the *.rb files?

Thanks,
Rob

Robert Gabaree <lists@...> writes:

If I have some files, say FOOD_DESCRIPTIONS and FOOD_GROUPS that I
need to parse and add their values to a database, where's the best
place to put them if I am doing it via migrations?

Is it bad to put them in the db/migrate folder along with the *.rb
files?

Migrations don't have to be to do with data structure. Any code can go into a
migration, including MyModel.create( {...} ) statements

The point is that you want these values to be inserted into the database at a
particular time, so you don't want them in a file that's loaded every time rails
starts, for example.

Use that as a starting point..

Gareth

I usually create a folder in db called data_import and put my data
import files and scripts there. You can use script/runner to execute
your load programs within the Rails environment.

ruby script/runner 'load "db/data_import/my_script.rb"'

Another way to run your data load program is with a custom rake
task. In lib/tasks create a file with a .rake extension and then
create a task:

desc 'Load my data.'
task :load_my_data => :environment do
   load "#{RAILS_ROOT}/db/data_import/my_script.rb"
end

Then run it:
rake load_my_data

The money line here is "task :load_standards => :environment do".
The => :environment says that this task requires the Rails
environment to be loaded before its run.

With either of these methods you can run your data load multiple
times without mucking up your migrations.

Aaron