gpt4 book ai didi

ruby-on-rails - 将 Geocoder Rails 与 Sidekiq 一起使用

转载 作者:可可西里 更新时间:2023-11-01 11:21:33 25 4
gpt4 key购买 nike

我想让 sidekiq 处理我的 geocoder

后台进程

我从来没有真正使用过 sidekiq,所以不用说我遇到了一些麻烦。我不断收到 undefined method geocoded_by 错误。我的代码如下。

Assets / worker /geocode_worker

class GeocodeWorker
include Sidekiq::Worker
extend ::Geocoder::Model::ActiveRecord

def perform
geocoded_by :address
reverse_geocoded_by :latitude, :longitude do |obj,results|
if geo = results.first
obj.city = geo.city
obj.zipcode = geo.postal_code

end
end
after_validation :geocode, :reverse_geocode


def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Location.create! row.to_hash
end
end
end
end

位置模型

class Location < ActiveRecord::Base
GeocodeWorker.perform_async
end

最佳答案

地理编码器的东西进入模型类:

class Location < ActiveRecord::Base 
geocoded_by :address
after_validation :geocode, :reverse_geocode
reverse_geocoded_by :latitude, :longitude do |obj,results|
if geo = results.first
obj.city = geo.city
obj.zipcode = geo.postal_code
end
end

def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Location.create! row.to_hash
end
end
end

然后我假设你的工作人员的任务是导入一个文件:

class ImportLocationWorker
include Sidekiq::Worker

def perform(file_path)
Location.import(file_path)
end
end

要启动工作人员导入文件,请调用:

ImportLocationWorker.perform_async("/some/path/to/file.csv")

关于ruby-on-rails - 将 Geocoder Rails 与 Sidekiq 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28160156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com