Search code examples
ruby-on-railsindexingassociationsrakethinking-sphinx

Thinking Sphinx - undefined method `klass'


I have an Artwork model that is associated to the models Subject, Location, Keyword, Style, and Medium by HABTM. I also have an association to an Artist model with one-to-many. Here's my error I keep getting:

>rake ts:index
Generating configuration to /Users/<user>/Developer/jtodd/jtoddgalleries/config/development.sphinx.conf
rake aborted!
NoMethodError: undefined method `klass' for nil:NilClass

Here's my indices file:

ThinkingSphinx::Index.define :artwork, :with => :active_record do
    indexes title, :sortable => true
    has jtg
    has width
    has height


    has subject.id, :as => :subject_ids
    has location.id, :as => :location_ids
    has keyword.id, :as => :keyword_ids
    has artist.first_name, :as => :artist_first
    has artist.last_name, :as => :artist_last

    has style.id, :as => :style_ids
    has medium.id, :as => :medium_ids
end

I can't figure out why I keep getting different errors. I may not have a firm grasp on fields vs. attributes and maybe that's where I'm going wrong. Any help is greatly appreciated, thank you!


Solution

  • I'm presuming your associations are all listed in your Artwork model with plural names? It needs to be the same in your index definition.

    As for fields vs attributes, a good rule of thumb is that anything you expect a user to type in and get results for should be a field. So, I'm guessing you'd want artist first and last names as fields.

    Thus, an amended index definition:

    ThinkingSphinx::Index.define :artwork, :with => :active_record do
      indexes title, :sortable => true
      indexes artist.first_name, :as => :artist_first
      indexes artist.last_name,  :as => :artist_last
      has jtg
      has width
      has height
    
      has subjects.id, :as => :subject_ids
      has locations.id, :as => :location_ids
      has keywords.id, :as => :keyword_ids
      has styles.id, :as => :style_ids
      has mediums.id, :as => :medium_ids
      # or is it media?
    end
    

    If you're still getting an error, can you run rake ts:index --trace and share the backtrace with us? :)