2012-01-08 63 views
1

我試圖做的是這裏所描述geosearching: http://freelancing-god.github.com/ts/en/geosearching.html思考獅身人面像 - 與HAS_ONE態關聯

除,而不是我的模型具有緯度和經度列,我想做的事:

class Post 
    has_one :location, :as => :locationable 
end 

class User 
    has_one :location, :as => :locationable 
end 

class Location 
    belongs_to :locationable, :polymorphic => true 
end 

使用戶和帖子都可以有位置記錄由locationable_type設置和locationable_id ......所以,我沒有在我的崗位模型如下:

define_index do 
    has locationable(:id), :as => :locationable_id 
    has "RADIANS(locations.lat)", :as => :latitude, :type => :float 
    has "RADIANS(locations.lng)", :as => :longitude, :type => :float 
end 

但我在'字段列表'錯誤中收到未知列...

我在做什麼錯?

回答

1

我想通了。這行:

has locationable(:id), :as => :locationable_id 

應該是強迫加入...但不能因爲沒有這樣的事情作爲郵寄記錄的定位它有一個位置..所以它需要:

define_index do 
    has location.locationable_id, :as => :locationable_id 
    has "RADIANS(locations.lat)", :as => :latitude, :type => :float 
    has "RADIANS(locations.lng)", :as => :longitude, :type => :float 
end 

現在它的工作。

0

它看起來你犯了一個錯誤錯字:(爲你寫的,你沒有「位置」,你有「locationable」)

define_index do 
    has locationable(:id), :as => :locationable_id 
    has "RADIANS(locationable.lat)", :as => :latitude, :type => :float 
    has "RADIANS(locationable.lng)", :as => :longitude, :type => :float 
end 

,並請確保您有緯度,經度列在「位置」表(選址模型)

+0

是的,我原本試過 - 再試一次,那也行不通。我是可疑的,它會工作,因爲從閱讀獅身人面像文檔,似乎當你做這樣的事情:「RADIANS(locationable.lat)」它會直接對話數據庫(沒有ruby/rails),並沒有這樣只要數據庫知道這個東西就可以定位 - 這是一個不可思議的軌道事物。實際的表名是「位置」,所以似乎獅身人面像需要引用它,不是嗎? – patrick 2012-01-08 18:39:40

+0

由於多態關係,您可能需要添加一些數據。 – mustafaturan 2012-01-08 21:32:20