レーキ タスク loaddata.rake があります。
require File.join(File.dirname(__FILE__), 'load_test_data.rb')
namespace :test do
desc "Insert test data into the database"
task(:loadtest => :environment) do
puts "environment = #{RAILS_ENV}"
puts "load_test_data "; load_test_data
puts "DONE!"
end
end
別のファイル load_test_data.rb があり、csv ファイルを読み取ってデータベースにデータを保存する関数がいくつかあります。そして、rake タスクからそのファイルを呼び出しています。
This is my load_test_data.rb
require "faster_csv"
def load_test_data
puts "test: Balance"
directory = "test/data"
name = "balances.csv"
lines = get_lines_from_csv(directory,name,"snapshot_errors")
unless lines.nil?
row_number = 1
lines.each do |row|
unless row.header_row?
begin
attributes = row.to_hash
attributes[:balance_name_id] = BalanceName.find(:first, :conditions => ["name = ?", attributes[:balance_name]]).id
attributes.delete(:balance_name)
Balance.create! attributes
rescue
store_error_in_table(row.fields.to_csv,name,row_number,"snapshot_errors")
end
row_number += 1
end
end
end
end
Rake タスクは、このコードで正常に動作しています..
今、私は遅れた仕事で私のレーキタスクからこの関数を呼び出したい..とにかく私はそれを処理できますか??