在我的控制台中,我正在执行'user = Zip.find(30052).users.first',它返回第一个用户。然后我运行命令'user.within_miles(25).count(:all)“,这样我就可以找到距离用户25英里范围内的所有人。这会产生错误,'未定义的局部变量或方法`zip''
似乎错误指向user.rb中缺少'within_miles'的方法。但我在那里定义了它。
user.rb:
scope :within_miles_of_zip, lambda{|radius, zip|
# Get the parameters for the search
area = zip.area_for(radius)
{ :select => "#{User.columns.map{|c| "users.#{c.name}"}.join(', ')}, sqrt(
pow(#{area[:lat_miles]} * (zips.lat - #{zip.lat}),2) +
pow(#{area[:lon_miles]} * (zips.lon - #{zip.lon}),2)) as distance",
:joins => :zip,
:conditions => "(zips.lat BETWEEN #{area[:min_lat]} AND #{area[:max_lat]})
AND (zips.lon BETWEEN #{area[:min_lon]} AND #{area[:max_lon]})
AND sqrt(pow(#{area[:lat_miles]} * (zips.lat - #{zip.lat}),2) +
pow(#{area[:lon_miles]} * (zips.lon - #{zip.lon}),2)) <= #{area[:radius]}",
:order => "distance"}
}
def within_miles(radius)
self.class.within_miles_of_zip(radius, zip)
end
答案 0 :(得分:1)
在这段代码中:
def within_miles(radius)
self.class.within_miles_of_zip(radius, zip)
end
未定义zip,这会导致您的错误。您应该考虑添加:
belongs_to :zip
到您的用户类。