【问题标题】:Searching through a collection of indexed model objects using Thinking Sphinx使用 Thinking Sphinx 搜索索引模型对象的集合
【发布时间】:2011-07-06 02:51:18
【问题描述】:

我有一个属于用户的技术模型,并被 Thinking Sphinx 索引。

我的模型中还有一个方法,它返回一个技术对象数组:

def possible_children(user)
    user.techniques - (self.children + [self])
end

这只是获取用户拥有的技术,减去已经是“self”技术对象的子项的技术以及“self”本身,然后返回剩余的技术对象。

然后在控制器中,我实例化一组可能的子对象,如下所示:

 @possible_children = @technique.possible_children(current_user).search params[:search]

这会返回一个“未定义的方法‘搜索’#”

不确定这是否相关,但发生这种情况的控制器不是 TechniquesController。

我要做的是搜索模型方法返回的任意集合。

有什么想法吗?

如果我需要提供更多信息,请告诉我。谢谢。

【问题讨论】:

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


    【解决方案1】:

    恐怕这对 Thinking Sphinx 来说是不可能的——至少,不是那么简单。你可以做的是使用你想要搜索的对象,获取它们的 id,然后在过滤器中使用它:

    possible_children = @technique.possible_children(current_user)
    Technique.search params[:search],
      :with => {:sphinx_internal_id => possible_children.collect(&:id)}
    

    Sphinx 有自己的 id,但是来自数据库的主键被 Thinking Sphinx 存储为属性sphinx_internal_id

    【讨论】:

    • :sphinx_internal_id!我在谷歌上找了半个小时。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多