0

我有一个Rails应用程序,其中我使用Thinking-Sphinx进行搜索,ActsAsTaggableOn进行标记。我希望能够在我的搜索查询中包含当前选定的标签。我尝试了以下,但没有得到它的工作。使用ActsAsTaggableOn思考狮身人面像标签搜索

在我的控制器:

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 

我为我的Idea模型索引:

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 

当我有一个标签选择我URLs是这样的:

/teams/1/tags/tag 

那么,我应该怎么做才能让Thinking-SphinxActsAsTaggableOn一起工作?

+0

您使用过rake ts:start和rake ts:index http://pat.github.io/thinking-sphinx/quickstart.html –

+0

@ThanatosSama是的,我已经完成了。 – Anders

回答

1

对于您的字段而言,您只能使用SQL支持的索引而不是实时索引。

在你的情况,你需要做的是建立在你的模型的方法,它返回所有的标签名称为一个字符串:

def tag_names 
    tags.collect(&:name).join(' ') 
end 

然后你可以参考,在您的索引定义:

indexes tag_names, :as => :tags 

一旦你做到了这一点,你需要重新生成狮身人面像索引,因为你已经改变了结构:rake ts:regenerate

+0

确实有效吗?来自思维狮身人面像文档(http://freelancing-gods.com/thinking-sphinx/indexing.html):“你不能引用模型方法 - Sphinx直接与你的数据库对话,而Ruby在这一点上不会被加载。” – Miotsu

+0

@Miotsu文档指的是SQL支持的索引(使用':with =>:active_record'),但这里问题中的索引使用实时索引(':with =>:real_time'),并且在那种情况下你只能*参考模型方法。 – pat

+0

现在我明白了,谢谢。 – Miotsu

相关问题