微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – delayed_job:使用handle_asynchronously时出现’undefined method’错误

我正在尝试使用delayed_job在我的rails数据库中运行更大的csv导入.这是我的控制器和模型方法

控制器方法

def import
    InventoryItem.import(params[:file],params[:store_id])
    redirect_to vendors_dashboard_path,notice: "Inventory Imported."
end

模型方法

def self.import(file,store_id)
CSV.foreach(file.path,headers: true) do |row|
inventory_item = InventoryItem.find_or_initialize_by_upc_and_store_id(row[0],store_id)
inventory_item.update_attributes(:price => row.to_hash["price"],:updated_at => "#{Time.Now}")
    end
end

handle_asynchronously :import

我已将’delayed_job’和’daemons’添加到我的gemfile中,然后捆绑.运行生成器,使用rake作业启动开发工作进程:工作,然后尝试通过应用程序运行导入.这是我得到的错误

Routing Error
undefined method `import' for class `InventoryItem'

在整合delayed_job时我错过了什么吗?这个导入过程之前运行得很好,所以只是想知道我搞砸了哪里.提前致谢!

解决方法

您的import是一个方法,您应该在模型类名的singleton类上调用handle_asynchronously:

您可以使用元类技巧来替换类方法

class << self
   def import(file,store_id)
     CSV.foreach(file.path,headers: true) do |row|
      inventory_item = InventoryItem.find_or_initialize_by_upc_and_store_id(row[0],store_id)
      inventory_item.update_attributes(:price => row.to_hash["price"],:updated_at => "#{Time.Now}")
     end
   end
  handle_asynchronously :import
end

希望这可以帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐