delayed_job:'未定义方法'错误时使用handle_asynchronously [英] delayed_job: 'undefined method' error when using handle_asynchronously
问题描述
我试图使用delayed_job来运行一个更大的csv导入到我的rails数据库。这是我的控制器和模型方法:
控制器方法
def import
InventoryItem.import(params [:file],params [:store_id])
redirect_to vendors_dashboard_path,通知:已导入库存。
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 jobs:work
启动一个开发工作进程,然后尝试通过应用程序运行导入。这是我得到的错误:
路由错误
未定义方法`import'类`InventoryItem'
整合delayed_job时,我错过了什么吗?这个导入过程运行得很好,所以只是想知道我在哪里搞砸了。提前感谢!
您的导入是一个类方法,您应该在模型类名称的singleton类上调用handle_asynchronously: p>
您可以使用metaclass技巧来对类方法进行别名:
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
$ b b
希望这有助于!
I'm attempting to use delayed_job to run a larger csv import into my rails database. Here are my controller and model methods:
controller method
def import
InventoryItem.import(params[:file], params[:store_id])
redirect_to vendors_dashboard_path, notice: "Inventory Imported."
end
model method
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
I've added 'delayed_job' and 'daemons' to my gemfile, then bundled. Ran the generator, started a development worker process with rake jobs:work
, and then tried to run an import through the app. Here's the error I get:
Routing Error
undefined method `import' for class `InventoryItem'
Did I miss something when integrating delayed_job? This import process ran fine prior, so just wondering where I've messed up. Thanks in advance!
Your import is a class method, you should call handle_asynchronously on your model class name's singleton class:
You can use the metaclass trick to alias class methods:
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
Hope this helps!
这篇关于delayed_job:'未定义方法'错误时使用handle_asynchronously的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!