Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a situation where I need to load data from CSV files that change infrequently and update data from the Internet daily. I'll include a somewhat complete example on how to do the former.</p> <hr> <p>First I have a rake file in <code>lib/tasks/update.rake</code>:</p> <pre><code>require 'update/from_csv_files.rb' namespace :update do task :csvfiles =&gt; :environment do Dir.glob('db/static_data/*.csv') do |file| Update::FromCsvFiles.load(file) end end end </code></pre> <p>The <code>=&gt; :environment</code> means we will have access to the database via the usual models.</p> <p>Then I have code in the <code>lib/update/from_csv_files.rb</code> file to do the actual work:</p> <pre><code>require 'csv' module Update module FromCsvFiles def FromCsvFiles.load(file) csv = CSV.open(file, 'r') csv.each do |row| id = row[0] s = Statistic.find_by_id(id) if (s.nil?) s = Statistic.new s.id= id end s.survey_area = row[1] s.nr_of_space_men = row[2] s.save end end end end </code></pre> <p>Then I can just run <code>rake update:csvfiles</code> whenever my CSV files changes to load the new data. I also have another task that is set up in a similar way to update my daily data.</p> <hr> <p>In your case you should be able to write some code to load your YML files or make your calculations directly. To handle your smaller corrections you could make a generic method for loading YML files and call it with specific files from the rake task. That way you only need to include the YML file and update the rake file with a new task. To handle execution order you can make a rake task that calls the other rake tasks in the appropriate order. I'm just throwing around some ideas now, you know better than me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload