思考狮身人面像 - 条件加入的索引(has_and_belongs_to_many)

时间:2014-05-14 11:47:05

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

我有模型Service,它具有按服务类型ID过滤服务的范围:

class Service < ActiveRecord::Base
  has_and_belongs_to_many :service_types  

  scope :by_service_types, -> ids { joins(:service_types).where('service_types_services.service_type_id in (?)', ids) }
end

class ServiceType < ActiveRecord::Base
  has_and_belongs_to_many :services
end

所以,当我运行范围时,我得到了这样的结果:

Service.by_service_types([54])
  Service Load (0.8ms)  SELECT "services".* FROM "services" INNER JOIN "service_types_services" ON "service_types_services"."service_id" = "services"."id" INNER JOIN "service_types" ON "service_types"."id" = "service_types_services"."service_type_id" WHERE "services"."deleted" = 'f' AND (service_types_services.service_type_id in (54))
 => ...

如何构建属性以构建此范围的索引?

1 个答案:

答案 0 :(得分:4)

首先,您希望服务类型ID作为服务索引定义中的多值属性:

ThinkingSphinx::Index.define(:service, :with => :active_record) do
  # ... existing index definition

  has service_types.id, :as => :service_type_ids
end

然后您可以搜索并使用该属性:

Service.search(:with => {:service_type_ids => 54})

如果你想将它包装成类似ActiveRecord范围的东西,Thinking Sphinx有自己的范围功能(两者不能组合,因为ActiveRecord适用于使用SQL查询的数据库,但Sphinx范围适用于使用SphinxQL进行Sphinx查询 - 类似,但不完全相同)。

# include this in your model:
include ThinkingSphinx::Scopes

sphinx_scope(:search_by_service_type) { |ids|
  {:with => {:service_type_ids => ids}}
}

然后搜索:

Service.search_by_service_type(54)
# You can chain further search arguments onto the scope:
Service.search_by_service_type(54).search('foo', :order => 'created_at DESC')