我正在使用geokit-Rails3gem在特定大学范围内的所有大学中查找产品。一所大学的has_many产品和一种产品属于大学,还有另一种类别模型,即has_many产品和产品的belongs_to类别。但是,当我尝试使用geokit从数据库中查找基于addess的大学时,它告诉我表中缺少lat coloumn。
大学迁移是
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))有什么建议可以解决这个问题吗?
发布于 2011-11-24 17:01:54
使用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-24 17:06:39
您还可以使用不同的列,并以这种方式将它们映射到acts_as_mappable:
acts_as_mappable :default_units => :miles,
:default_formula => :sphere,
:distance_field_name => :distance,
:lat_column_name => :lat,
:lng_column_name => :lnghttps://stackoverflow.com/questions/8254335
复制相似问题