1

私は geokit-rails3 gem を使用して、特定の大学の範囲内のすべての大学で製品を見つけています。大学には多くの製品があり、製品は大学に属しています。別のカテゴリ モデルでは、多くの製品があり、製品がカテゴリに属しています。しかし、ジオキットを使用して住所に基づいてデータベースから大学を見つけようとすると、テーブルに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 に答える