如何从思考sphinx中将额外的字段加载到模型中?

时间:2012-08-03 23:59:29

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

我像这样使用思考 - 狮身人面像:

class Project < ActiveRecord::Base
  define_index do
    indexes title
    indexes comments.message, as: :comment_message
    indexes category_projects.description, as: :category_description
  end

  class << self
    def text_search(word)
      ThinkingSphinx.search(word, include: :comments)
    end
  end
end

当我这样做时Project.text_search('test')它有效,但我想这样做:

@projects = Project.text_search('test')
@projects.each do |project|
  puts project.comment_message
end

目前我收到了这条消息:

undefined method `comment_message' for #<ThinkingSphinx::Search:0x000000057e11c0>
    from /home/dougui/.rvm/gems/ruby-1.9.3-p194-perf@comment_my_projects/bundler/gems/thinking-sphinx-b293abdbdf0c/lib/thinking_sphinx/search.rb:185:in `method_missing'
    from (irb):1
    from /home/dougui/.rvm/gems/ruby-1.9.3-p194-perf@comment_my_projects/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
    from /home/dougui/.rvm/gems/ruby-1.9.3-p194-perf@comment_my_projects/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
    from /home/dougui/.rvm/gems/ruby-1.9.3-p194-perf@comment_my_projects/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

那么,如何从think-sphinx中将字段加载到模型中?

我没有找到任何文档。

谢谢!

1 个答案:

答案 0 :(得分:0)

在TS邮件列表上也回答了,但实质上,Sphinx并不返回这些字段,因此您只需要参考构建字段的关联/列:

projects.each do |project|
  puts project.comments.message.join("\n")
end