【问题标题】:Relation won't index关系不会索引
【发布时间】:2011-08-14 17:08:30
【问题描述】:

我在索引我的一个模型时遇到了一些问题。

这是我的模型:

class Model < ActiveRecord::Base
  define_index do
    # ...
    has tracker.open, as: :open, type: :boolean
    has source.priority, as: :priority, type: :integer
    # ...
  end

  belongs_to :tracker
  belongs_to :source
end

我正在运行它来索引模型:

rake thinking_sphinx:index --trace

这是错误:

undefined method `priority' for #<ThinkingSphinx::Source:0x00000106ae1738>

任何人都知道为什么tracker 关系有效,但不知道source
我在 OS X 10.7 中使用 Sphinx 0.9.9-release、Rails 3.1.0.rc5。

更新

在使用此 rake 任务(reindex 而不是 index)时,我在索引部分期间没有收到任何错误。

rake thinking_sphinx:reindex

现在的问题是我不能使用优先级字段。 这是搜索时使用的代码:

Model.search(with: {priority: [1]})

这行代码:

has source(:priority)

导致此错误:

ArgumentError: wrong number of arguments (1 for 0) # Produced by the line above.

使用这条线:

has source.priority

导致此错误:

NoMethodError: undefined method `priority' for #<ThinkingSphinx::Source:0x00000106b0ff98>

有人知道为什么吗?

更新 2

使用rake thinking_sphinx:rebuild 重新索引数据库,而不是rake thinking_sphinx:indexrake thinking_sphinx:reindex

【问题讨论】:

  • 看起来 Thinking-Sphinx 有 Source 类,你有 Source 模型,TS 无法解决这个问题。试试has source(:priority) ...
  • 感谢您的提示,但没有成功。我更新了我的帖子。
  • 尝试重命名您的Source 模型。 TS有自己的source方法(rubydoc.info/gems/thinking-sphinx/2.0.5/ThinkingSphinx/…)
  • 我不会只为这样的小事重命名模型,而是重命名关联以避免命名空间冲突:belongs_to :source_thingy, :class_name =&gt; "Source"
  • @David Grayson 问题是 sphinx 没有使用 ActiveRecord 来索引数据库。它(根据手册)直接与数据库对话。我得到的错误现在是ERROR: index 'model_core': sql_range_query: Unknown column 'model.thingy_id' in 'on clause' :( 无论如何谢谢。

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


【解决方案1】:

如列表中所述,您需要使用assoc 方法来解决此问题:

has assoc(:source).priority

这里的问题是,在后台,Sphinx 索引有源 - 因此 Thinking Sphinx 自动构建源,因此保留源方法。我真的应该改变它,但同时这会让你绕过它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    相关资源
    最近更新 更多