2012-01-08 62 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