Delay job - undefined method 'name' for nil: NilClass
I am struggling with this error. I have a controller action like this:
def import
InventoryItem.import_list(params[:file], current_vendor.id)
redirect_to vendors_dashboard_path, notice: "Inventory Imported"
end
My class method:
class << self
def import_list(file, vendor_id)
vendor = Vendor.find(vendor_id)
SmarterCSV.process(file.path,
{:key_mapping => { vendor.import_preference.brand_symbol => :brand,
vendor.import_preference.product_symbol => :product,
vendor.import_preference.price_symbol => :price,
vendor.import_preference.upc_symbol => :upc}}) do |row|
params = row.first.merge(:store_id => vendor.stores.first.id)
inventory_item = InventoryItem.create(params)
end
end
handle_asynchronously :import_list
end
When I try to execute I get undefined method error. This method works without being called handle_asynchronously
and when I do it with a normal class method self.import
. What am I doing wrong here?
* UPDATE **
Server error log:
Started POST "/inventory_items/import" for 127.0.0.1 at 2014-10-06 09:30:53 -0700
Processing by InventoryItemsController#import as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "file"=>#<ActionDispatch::Http::UploadedFile:0x000001081d3ab8 @original_filename="Inventory1 - Sheet1.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"Inventory1 - Sheet1.csv\"\r\nContent-Type: text/csv\r\n", @tempfile=#<Tempfile:/var/folders/6Q/6Qj+aFh4A4orYbGW-nE+++TI/-Tmp-/RackMultipart201406-65416-1dcpxp9>>, "commit"=>"Import"}
Vendor Load (0.9ms) SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" = 25 LIMIT 1
Completed 500 Internal Server Error in 99ms
NoMethodError (undefined method `name' for nil:NilClass):
app/controllers/inventory_items_controller.rb:6:in `import'
Relevant stack trace:
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:50:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `yield'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:480:in `block in dump_ivars'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:478:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:478:in `dump_ivars'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:150:in `visit_Object'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:344:in `block in visit_Array'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:344:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:344:in `visit_Array'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:467:in `block in emit_coder'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:465:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:465:in `emit_coder'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:450:in `dump_coder'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:126:in `accept'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:92:in `push'
/Users/myname/.rvm/rubies/ruby-2.0.0-p576/lib/ruby/2.0.0/psych.rb:244:in `dump'
delayed_job (4.0.4) lib/delayed/backend/base.rb:81:in `payload_object='
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.13) lib/active_record/base.rb:498:in `initialize'
delayed_job (4.0.4) lib/delayed/backend/base.rb:32:in `new'
delayed_job (4.0.4) lib/delayed/backend/base.rb:32:in `enqueue'
delayed_job (4.0.4) lib/delayed/message_sending.rb:12:in `method_missing'
delayed_job (4.0.4) lib/delayed/message_sending.rb:46:in `block in handle_asynchronously'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
source to share
params[:file]
will not be configured correctly when saved to the database for delayed_job. Instead of passing an object params[:file]
as the first argument, pass the file path instead:
def import
InventoryItem.import_list(params[:file].path, current_vendor.id)
redirect_to vendors_dashboard_path, notice: "Inventory Imported"
end
Then just use the path directly in your working method:
class << self
def import_list(path, vendor_id)
vendor = Vendor.find(vendor_id)
SmarterCSV.process(path, {:key_mapping => { vendor.import_preference.brand_symbol => :brand, vendor.import_preference.product_symbol => :product, vendor.import_preference.price_symbol => :price, vendor.import_preference.upc_symbol => :upc}}) do |row|
params = row.first.merge(:store_id => vendor.stores.first.id)
inventory_item = InventoryItem.create(params)
end
end
handle_asynchronously :import_list
end
source to share