思考Sphinx - 使用has_one多态关联进行geosearching

时间:2012-01-08 08:19:31

标签: ruby-on-rails ruby-on-rails-3 thinking-sphinx

我正在尝试做这里描述的内容: http://freelancing-god.github.com/ts/en/geosearching.html

除了在我的模型上设置纬度和经度列之外,我想做:

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

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

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

这样User和Post都可以通过locationable_type和locationable_id设置位置记录......所以我在Post模型中做了以下内容:

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

但是我在'字段列表'错误中收到了一个未知列...

我做错了什么?

2 个答案:

答案 0 :(得分: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

现在可行。

答案 1 :(得分:0)

它看起来你犯了错误的错误:(正如你所写,你没有'地点',你有'可定位')

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

请确保您在'locations'表(位置模型)中有lat,lng列