ruby-on-rails - Geokit plus Rails 3.1.1,纬度和经度问题。

标签 ruby-on-rails ruby ruby-on-rails-3 geokit

我正在使用 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/

相关文章:

ruby-on-rails - ActiveRecord:除非另有说明,否则在保存之前使所有文本字段都调用 strip

ruby-on-rails - Rails if 语句导致非法八进制数字错误?

ruby-on-rails - FactoryGirl 与 Refile gem

sql - Arel刚刚发生了什么,我该如何使用Arel::SelectManager?

css - Ruby on Rails - 在 css erb 文件中使用助手给出未定义的方法

sql - 如何使用 hstore 进行 IN 查询?

jquery - 如何使用 jQuery 和 UJS 通过 AJAX 以 RJS 方式加载 HTML

ruby-on-rails - rails 上的 ruby : controller can't access newly added record

ruby-on-rails - ruby 中是否有更短的方法来获取两个日期中的较晚日期?

ruby-on-rails - Rails 中未定义的方法 "_path"