【发布时间】:2011-11-10 00:24:17
【问题描述】:
我通过以下 rake 任务获得了 undefined local variable or method 'address_geo' for main:Object。它有什么问题?
include Geokit::Geocoders
namespace :geocode do
desc "Geocode to get latitude, longitude and address"
task :all => :environment do
@spot = Spot.find(:first)
if @spot.latitude.blank? && !@spot.address.blank?
puts address_geo
end
def address_geo
arr = []
arr << address if @spot.address
arr << city if @spot.city
arr << country if @spot.country
arr.reject{|y|y==""}.join(", ")
end
end
end
【问题讨论】:
-
看起来您在定义函数之前调用了它 - 您是否尝试将
address_geo的函数定义移动到任务的顶部? -
是的,试过了,但没用。但我想我不再需要这个了。谢谢。
-
大多数时候我想在 rake 任务中定义一个方法,我发现通过创建一个新的 rake 任务并调用它可以更好地解决问题:
Rake::Task['cache:clear'].invoke。它更易于阅读和维护,您可以单独运行rake cache:clear。
标签: ruby-on-rails ruby methods rake