我正在使用 geokit-rails3 gem 在特定大学范围内的所有大学中查找产品。一个学院有很多产品,一个产品属于学院,还有一个类别模型有很多产品,产品属于类别。但是,当我尝试使用 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
Controller
@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))
任何提示如何解决这个问题?
最佳答案
任何 acts_as_mappable with geokit 都需要一个 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
关于ruby-on-rails - Geokit plus Rails 3.1.1,纬度和经度问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254335/