0

我是新手,我想知道是否可以使用参数定义模型方法。我是说。我有这种方法可以使用球坐标计算距离

#in my model

#Haversin formula to calculate distance between spherical coordinates 
def self.distance(b)                                                
  rad_per_deg = Math::PI/180  # PI / 180                                                      
  rkm = 6371                  # Earth radius in kilometers                                    
  #rm = rkm * 1000            # Radius in meters                                              
  a=[]                                                                                        
  a.push(self.lat)                                                                            
  a.spuh(self.long)                                                                           

  dlon_rad = (b[1]-a[1]) * rad_per_deg  # Delta, converted to rad                             
  dlat_rad = (b[0]-a[0]) * rad_per_deg                                                        

  lat1_rad, lon1_rad = a.map! {|i| i * rad_per_deg }                                          
  lat2_rad, lon2_rad = b.map! {|i| i * rad_per_deg }                                          

  a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2

  c = 2 * Math.asin(Math.sqrt(a))                                                             

  distance=rkm * c                                                                            
  return distance                                                                             
end                                                                                           

我希望它像这样工作: obj.distance(b) 其中 b 是纬度和经度数组。但是当我在 irb 上尝试这个时,我得到:

NoMethodError: undefined method `distance' for #<Object:0x000000058854c8>

可能我错过了一些东西。

class Meteo < ActiveRecord::Base                                                                
  attr_accessible :date, :humidity, :lat, :long, :pressure, :temp, :town, :wind, :wind_direction
, :rain_quantity                                                                                

  #Haversin formula to calculate distance between spheric coordinates                           
  def self.distance(b)                                                                          
    rad_per_deg = Math::PI/180  # PI / 180                                                      
    rkm = 6371                  # Earth radius in kilometers                                    
    #rm = rkm * 1000            # Radius in meters                                              
    a=[]                                                                                        
    a.push(self.lat)                                                                            
    a.spuh(self.long)                                                                           

    dlon_rad = (b[1]-a[1]) * rad_per_deg  # Delta, converted to rad                             
    dlat_rad = (b[0]-a[0]) * rad_per_deg                                                        

    lat1_rad, lon1_rad = a.map! {|i| i * rad_per_deg }                                          
    lat2_rad, lon2_rad = b.map! {|i| i * rad_per_deg }                                          

    a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)
**2                                                                                             
    c = 2 * Math.asin(Math.sqrt(a))                                                             

    distance=rkm * c                                                                            
    return distance                                                                             
  end                                                                                           
end                                                                                             

我在 irb 上这样称呼它:

irb> m=Meteo.last

irb> b=[86.43971008189519, 23.477053751481986]

irb> m.distance(b)

4

2 回答 2

5

只需删除self.

当您编写 时def self.distance,您的意思是该方法将在模型类上调用。def distance如果您希望在模型实例上调用该方法,则应该使用。

比较:

class SomeModel
  def self.distance
    # ...
  end
end

SomeModel.distance

和:

class SomeModel
  def distance
    # ...
  end
end

obj = SomeModel.new
obj.distance
于 2013-08-29T12:56:43.607 回答
2

如果我猜对了,您正在定义一个类方法(使用def self.distance),但在该类的实例上调用它(obj.distance(array))。

您应该在 obj 的类上调用该方法,例如Meteo.distance(array). 或者简单地将其定义为实例方法,只需保留selfin 方法定义即可。

希望,这有帮助

于 2013-08-29T12:54:05.610 回答