Relation won't index

旧城冷巷雨未停 提交于 2019-12-13 06:58:33

问题


I'm having some problem indexing one of my models.

Here is my model:

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

I'm running this to index the model:

rake thinking_sphinx:index --trace

Here is the error:

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

Anyone knows why the tracker relations works, but not the source?
I'm using Sphinx 0.9.9-release, Rails 3.1.0.rc5 in OS X 10.7.

Update

I don't get any errors during the indexing-part when using this rake task (reindex instead of index).

rake thinking_sphinx:reindex

The problem is now that I can't use the priority field. This is the code used when searching:

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

This line of code:

has source(:priority)

Results in this error:

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

Using this line:

has source.priority

Results in this error:

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

Anyone knows why?

Update 2

Used rake thinking_sphinx:rebuild to reindex the database, instead of rake thinking_sphinx:index and rake thinking_sphinx:reindex.


回答1:


As discussed on the list, you need to use the assoc method to work around this:

has assoc(:source).priority

The issue here is that under the hood, Sphinx indices have sources - and so Thinking Sphinx is constructing sources automatically, hence the reserved source method. I really should change that, but this will get you around it in the meantime.



来源:https://stackoverflow.com/questions/7058338/relation-wont-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!