我有两个模型,Location
并且Basic
,我正在使用pg_search和geocoder gem,如何使用 geocoder 方法将范围添加到我pg_search_scope
的方法 near
中,或者如果有另一种方法可以这样做?
Location
模型具有纬度和经度列。
在我的控制器中,我能够做到这一点:
# this allows me to search near the Location model
@business = Location.near(params[:loc], 15)
# this searches through the pg_search_scope
@term = Basic.search(params[:q])
#I'm trying to combine @business and @term
有了Basic
模型,我有这个:
pg_search_scope :search, :against => [:first_name, :last_name],
:associated_against => {
:services => [:service, :description]
},
:using => {
:tsearch => {:disctionary => "english" }
}
我想将两者合二为一,@business
这可能吗?@term
(@search)
谢谢
++++++++++++++++++
所以在我的Basic
模型中:
class Basic < ActiveRecord::Base
belongs_to :location
has_many :services, as: :servicable
pg_search_scope :search, :against => [:first_name, :last_name],
:associated_against => {
:services => [:service, :description]
},
:using => {
:tsearch => {:disctionary => "english" }
}
end
所以在我的模型中,我有与基本相关的服务。因此,当我搜索名称或服务时,结果会弹出。但我试图只在用户输入的某个位置附近显示结果,这就是为什么我的表location_id
中有一个列Basic
。
这是我的Location
模型
class Location < ActiveRecord::Base
has_many :basics
geocoded_by :new_address
after_validation :geocode, :if => :new_address_changed?
def gmaps4rails_address
:new_address
end
def new_address_changed?
attrs = %w(street city state)
attrs.any?{|a| send "#{a}_changed?"}
end
end
所以我试图链接这些方法。理想情况下,应用程序将首先搜索所有附近的结果,然后搜索附近结果中匹配的术语,然后仅显示适用的术语。