1

我正在使用 geokit-rails3 gem 在特定大学范围内的所有大学中查找产品。一个学院 has_many products 和一个产品属于学院,还有另一个类别模型 has_many products 和 product belongs_to category。但是,当我尝试使用 geokit 根据地址从数据库中查找大学时,它告诉我表中缺少 lat 列。

大学移民是

 create_table :colleges do |t|
  t.string :name
  t.text :address
  t.string :city
  t.string :state
  t.integer :zipcode

  t.timestamps

控制器

     @products = College.within(5, :origin=>@current_product.college.address).product

错误:

     Mysql::Error: Unknown column 'colleges.lat' in 'field list': SELECT `colleges`.*, 
      (ACOS(least(1,COS(0.3223824452162744)*COS(1.2891920858347756)*COS(RADIANS(colleges.lat))*COS(RADIANS(colleges.lng))+
      COS(0.3223824452162744)*SIN(1.2891920858347756)*COS(RADIANS(colleges.lat))*SIN(RADIANS(colleges.lng))+
      SIN(0.3223824452162744)*SIN(RADIANS(colleges.lat))))*3963.19)
      AS distance FROM `colleges`  WHERE ((colleges.lat>18.398868573573203 AND colleges.lat<18.543438426426793 AND colleges.lng>73.78905443427034 AND colleges.lng<73.94147656572967)) AND ((
      (ACOS(least(1,COS(0.3223824452162744)*COS(1.2891920858347756)*COS(RADIANS(colleges.lat))*COS(RADIANS(colleges.lng))+
      COS(0.3223824452162744)*SIN(1.2891920858347756)*COS(RADIANS(colleges.lat))*SIN(RADIANS(colleges.lng))+
      SIN(0.3223824452162744)*SIN(RADIANS(colleges.lat))))*3963.19)
      <= 5))

任何提示如何解决这个问题?

4

2 回答 2

3

任何带有 geokit 的acts_as_mappable 都需要一个 lat & lnt 字段(可以使用不同的名称覆盖这些字段)

见顶部: http: //geokit.rubyforge.org/readme.html

我建议:

add_column :colleges, :lat, :float
add_column :colleges, :lng, :float
add_index  :colleges, [:lat, :lng]

此外,要自动更新地址:

before_save :update_location

def update_location
 #you probably you to use a full address, or join all the address parts instead of just self.address
 loc=Geocoder.geocode(self.address)
 if loc.success
   self.lat = loc.lat
   self.lng = loc.lng
 end
end
于 2011-11-24T09:01:53.780 回答
0

您还可以有不同的列,并以这种方式将它们映射到acts_as_mappable:

acts_as_mappable  :default_units => :miles,
                  :default_formula => :sphere,
                  :distance_field_name => :distance,
                  :lat_column_name => :lat,
                  :lng_column_name => :lng
于 2011-11-24T09:06:39.123 回答