views:

26

answers:

1

I have a rake task 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

I have another file load_test_data.rb there i have some functions to read csv file and store data in database. And i am calling that file from my rake task.

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 task is working fine with my this code..

Now i want to call this function from my rake task with delayed job.. Is there anyway i can handle it??

A: 

You can call it by system command

system 'rake test:loadtest'

shingara