Ruby on rails 用Actsastagleon标签搜索思考斯芬克斯
我有一个Rails应用程序,其中我使用Ruby on rails 用Actsastagleon标签搜索思考斯芬克斯,ruby-on-rails,ruby,ruby-on-rails-4,thinking-sphinx,acts-as-taggable-on,Ruby On Rails,Ruby,Ruby On Rails 4,Thinking Sphinx,Acts As Taggable On,我有一个Rails应用程序,其中我使用Thinking Sphinx进行搜索,使用ActsaStaggaleon进行标记。我希望能够在搜索查询中包含当前选定的标记。我尝试了以下方法,但没有成功 在我的控制器中: def show @team_tags = @team.ideas.tag_counts_on(:tags) if params[:tag] @ideas = @team.ideas.search(params[:search], :conditions =>
Thinking Sphinx
进行搜索,使用ActsaStaggaleon
进行标记。我希望能够在搜索查询中包含当前选定的标记。我尝试了以下方法,但没有成功
在我的控制器中:
def show
@team_tags = @team.ideas.tag_counts_on(:tags)
if params[:tag]
@ideas = @team.ideas.search(params[:search], :conditions => { tags: "tag" })
else
@ideas = @team.ideas.search(params[:search])
end
end
我的想法
模型的索引:
ThinkingSphinx::Index.define :idea, :with => :real_time do
[...]
indexes tags.name, :as => :tags
has user_id, type: :integer
has team_id, type: :integer
[...]
end
这给了我以下错误:
ActionView::Template::Error (index idea_core: query error: no field 'tags' found in schema
选择标记后,我的URL
如下所示:
/teams/1/tags/tag
那么,我应该怎么做才能让
Thinking Sphinx
和ActsaStaggaleon
一起工作呢?你为你的领域所做的将只适用于SQL支持的索引,而不是实时索引
在您的情况下,您要做的是在模型中创建一个方法,将所有标记名作为单个字符串返回:
def tag_names
tags.collect(&:name).join(' ')
end
然后,您可以在索引定义中引用:
indexes tag_names, :as => :tags
完成后,您需要重新生成Sphinx索引,因为您已经更改了结构:
rake ts:regenate
您是否使用了rake ts:start和rake ts:index@ThanatosSama是的,我已经这样做了。这真的有效吗?思考Sphinx文档():“您无法引用模型方法-Sphinx直接与您的数据库对话,Ruby此时无法加载。”@Miotsu这些文档引用的是SQL支持的索引(使用:with=>:active_record
),但这里的索引使用的是实时索引(:with=>:real_time
),在这种情况下,您只能引用模型方法。