Ruby on rails 延迟工作:';未定义的方法';异步使用句柄\u时出错
我正在尝试使用延迟的_作业将更大的csv导入到我的rails数据库中。以下是我的控制器和模型方法: 控制器方法Ruby on rails 延迟工作:';未定义的方法';异步使用句柄\u时出错,ruby-on-rails,ruby,csv,delayed-job,Ruby On Rails,Ruby,Csv,Delayed Job,我正在尝试使用延迟的_作业将更大的csv导入到我的rails数据库中。以下是我的控制器和模型方法: 控制器方法 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(f
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
我在gemfile中添加了'delayed_job'和'daemon',然后打包。运行生成器,使用rake jobs:work
启动开发工作进程,然后尝试通过应用程序运行导入。下面是我得到的错误:
Routing Error
undefined method `import' for class `InventoryItem'
我在工作中错过了什么吗?这个导入过程之前运行得很好,所以我只是想知道我把事情搞砸了。提前谢谢 您的导入是一个类方法,您应该异步调用模型类名的单例类上的handle\u: 您可以使用元类技巧来别名类方法:
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
class row.to_hash[“price”],:updated_at=>“#{Time.now}”)
结束
结束
异步处理\u:导入
结束
希望这有帮助 因此,如果我理解正确,我会将导入方法包装在上述别名中,
inventory\u item.rb
?现在,我在InventoryItemsController#import错误中得到了一个NoMethod错误,指向InventoryItem
上调用import
的行。想法?@settheline我认为您遇到的错误是由于在将方法定义封装到“类”中后没有删除方法定义前面的“self”