Rails:使用模型关联进行太阳黑子文本搜索,使用:through
如何搜索关联和太阳黑子?
class StaticController 10 order_by_geodist(:location, *Geocoder.coordinates(params[:loc])) end @biz = @search.results end
class Business :professionals searchable do text :name #name in business column # how to do I get the services? end end
class Service < ActiveRecord::Base attr_accessible :service belongs_to :professional end
class Professional < ActiveRecord::Base belongs_to :business has_many :services, as: :servicable end
在视图中,我有这个(很多循环)
如果我搜索商业模型中的名称,这会有效,但如果我正在搜索Service
模型中的术语,该怎么办? 它无法正确显示,因为我的观点仅来自业务方面。 如何搜索Service
模型,如何创建商业名称?
谢谢
您需要为调用模型中的关联模型创建其他索引才能实现此目的。 例如:
class Business < ActiveRecord::Base attr_accessible :name has_many :services, :through => :professionals searchable do text :name #name in business column text :services do # this one for full text search services.map(&:service).compact.join(" ") end string :services , :multiple => true do #this one for exact searches services.map(&:service).compact end end end
之后,您可以执行以下查询:
Bussines.search do with(:services, "some_service") end.execute.results
现在,您不再需要连接mysql表来获取数据。 您可以从solr中获取数据。 这是solr的最大优势之一。
我希望这说清楚。 如果您需要更多详细信息,请随意删除评论。