-->

Relation won't index

2019-08-27 02:46发布

问题:

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.