加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – delayed_job:使用handle_asynchronously时出

发布时间:2020-12-17 01:47:59 所属栏目:百科 来源:网络整理
导读:我正在尝试使用delayed_job在我的rails数据库中运行更大的csv导入.这是我的控制器和模型方法: 控制器方法 def import InventoryItem.import(params[:file],params[:store_id]) redirect_to vendors_dashboard_path,notice: "Inventory Imported."end 模型方
我正在尝试使用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

希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读