在具有以下配置的关联模型上定义索引的正确方法是什么?
我有型号 Locality
与 lat
和 lng
属性和相关模型 Profile
和 User
class User < ActiveRecord::Base
has_one :user_profile
define_index do
# This doesn't work :(
has "RADIANS(user_profiles.localities.lat)", :as => :lat, :type => :float
has "RADIANS(user_profiles.localities.lng)", :as => :lng, :type => :float
end
end
end
class UserProfile < ActiveRecord::Base
belongs_to :user
belongs_to :locality
end
class Locality < ActiveRecord::Base
has_many :user_profiles
end
我需要为 User 模型定义索引,以便我可以对其执行地理搜索。
谢谢你的回答!
最佳答案
问题是双重的:
以下应该可以解决问题:
define_index do
# force the join on the associations
join user_profile.locality
# normal SQL:
has "RADIANS(localities.lat)", :as => :lat, :type => :float
has "RADIANS(localities.lng)", :as => :lng, :type => :float
end
Claudio 在 SQL 中使用单数引用的观点是不正确的 - 在 SQL 中,您需要表名。但是您确实希望在 join 调用中使用正确的关联引用(因此它们在那里是单数)。
干杯
关于ruby-on-rails - 使用 Thinking Sphinx 定义关联模型的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4993939/