Ruby on rails 4 Rails 4-使用相关模型上的字段思考Sphinx过滤器
在我的Rails 4应用程序中,我有如下型号: “代理组”Ruby on rails 4 Rails 4-使用相关模型上的字段思考Sphinx过滤器,ruby-on-rails-4,thinking-sphinx,Ruby On Rails 4,Thinking Sphinx,在我的Rails 4应用程序中,我有如下型号: “代理组” has_many :group_agencies has_many :agencies, :through => :group_agencies 我保存“令牌”字段的位置 “代理”模式 has_many :group_agencies has_many :agency_groups, :through => :group_agencies has_many :advertisements “广告”模式 b
has_many :group_agencies
has_many :agencies, :through => :group_agencies
我保存“令牌”字段的位置
“代理”模式
has_many :group_agencies
has_many :agency_groups, :through => :group_agencies
has_many :advertisements
“广告”模式
belongs_to :agency
我使用Thinking Sphinx,它的效果非常好,但现在我有了新的要求,需要通过代理组令牌字段过滤“广告”
基本上,我需要找到一些参数的广告,但只为机构组中的机构张贴代币
if params[:search]
@results = Advertisement.search Riddle::Query.escape(params[:search]), :star => true, :page => params[:page], :per_page => 6
end
要获得结果,我运行http查询,如下所示:
http://api.localhost.local:3000/v1/advertisements?token=JHW_tdXn5g-vQY1f_ZzLuw&search=Nissim
has agency_group_ids, :type => :integer, :multi => true
我错过了什么?如何在TS中使用模型之间的关系?我认为最好的方法包括以下几个步骤: 步骤1:在广告索引中添加代理组ID作为属性。如果您使用的是SQL支持的索引(
:with=>:active_record
),那么它是一个单行程序:
has agency.group_agencies.agency_group.id, :as => :agency_group_ids
如果您使用的是实时索引,那么您需要在播发中使用一个方法来返回所有这些ID:
def agency_group_ids
agency.agency_group_ids
end
属性定义如下所示:
http://api.localhost.local:3000/v1/advertisements?token=JHW_tdXn5g-vQY1f_ZzLuw&search=Nissim
has agency_group_ids, :type => :integer, :multi => true
步骤2:由于您已更改了索引结构,请不要忘记重建索引:
# for SQL-backed indices:
rake ts:rebuild
# or, for real-time indices
rake ts:regenerate
步骤3:在控制器中,查找给定令牌的代理组:
agency_group = AgencyGroup.find_by :token => params[:token]
步骤4:最后,在搜索通话中使用该代理组的id:
@results = Advertisement.search Riddle::Query.escape(params[:search]),
:star => true,
:page => params[:page],
:per_page => 6,
:with => {:agency_group_ids => agency_group.id}
伟大的工作完美。非常感谢你的回答。清洁和简单的解决方案。完美的