2011-11-24 3 views
1

geokit-rails3 gem을 사용하여 특정 대학 범위 내의 모든 대학에서 제품을 찾습니다. 대학에는 has_many 제품과 제품이 대학에 속합니다. has_many 제품과 products belongs_to 카테고리가있는 또 다른 카테고리 모델이 있습니다. 그러나 geokit을 사용하여 addess를 기반으로 데이터베이스에서 대학을 찾으려고 할 때 저 테이블에 lat coloumn이 없다고 알려줍니다.Geokit plus Rails 3.1.1 위도 및 경도 문제.

대학 마이그레이션은

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)) 

어떤 힌트? http://geokit.rubyforge.org/readme.html

내가 권하고 싶습니다 :

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

geokit와 acts_as_mappable

+0

위 열이 있습니까? geokit-rails3이 당신을 위해 생성하지 않는 것 같습니다. – tommasop

+0

위와 같은 경우 lat 열이 없습니다 –

답변

3

뭐든지 위도 & LNT 필드 (이 서로 다른 이름을 사용하여 대체 할 수 있습니다)

이의 상단을 볼 필요가있다 또한 주소 자동 업데이트 :

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 
+0

새 대학을 만들 때 수동으로 lat와 lng 값을 데이터베이스에 추가해야합니까? –

+1

네,하지만 이것을 자동화 할 수 있습니다, 위의 코멘트를 업데이트 할 것입니다. – easyjo

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