views:

153

answers:

2

My controller data_files_controller.rb

def upload_balances
  DataFile.load_balances(params)
end

My model data_file.rb def self.load_balances(params)

#  Pull the file out of the http request, write it to file system
name =  params['Filename']
directory = "public/uploads"
errors_table_name = "snapshot_errors"
upload_file = File.join(directory, name)
File.open(upload_file, "wb") { |f| f.write(params['Filedata'].read) }
# Remove the old data from the table
Balance.destroy_all
------ more code-----
end

It's working fine. Now i want to use delayed job with my controller to call my model action like .. My controller data_files_controller.rb

def upload_balances
  DataFile.send_later(:load_balances,params)      
end

Is it possible?? What's the other way to do it? Is it create any problem?

With this send_later i am getting this error in column last_error in delayed_job table.

uninitialized stream C:/cyncabc/app/models/data_file.rb:12:in read' C:/cyncabc/app/models/data_file.rb:12:inload_balances' C:/cyncabc/app/models/data_file.rb:12:in `open'

I am getting error on line

File.open(upload_file, "wb") { |f| f.write(params['Filedata'].read) }

while reading params['Filedata'].read.

how to check i am getting proper data in params['Filedata'] or not ? Without send_later it's working fine... Is there any solution?

A: 

using send_later should work fine. The other way is to define your own class that responds to perform, and call Delayed::Job.enqueue YourClass.new

Did you have errors somewhere?

x1a4
I have edited my post with errors. is there any solution?
krunal shah
A: 

You should check what's in the database. Delayed Job must have strings, or ids internally when running it: most probably

params['Filedata']

contains something that is not revivable when the job runs later.

Will Sargent
If user upload any csv file than first load_balance function will create that file in public/upload and write content in it from the original file. Than with faster csv it will enter the records from the file to database. But it's not storing anything params['Filedata'] or params['Filename'] in the database.
krunal shah