Thinking Sphinx - 未定义的方法`klass'

Thinking Sphinx - undefined method `klass'

我有一个 Artwork 模型关联到模型 SubjectLocationKeywordStyleMedium通过 HABTM。我还与具有一对多的 Artist 模型有关联。这是我不断收到的错误:

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

这是我的索引文件:

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

我不明白为什么我总是收到不同的错误。我可能对字段与属性没有牢固的把握,也许这就是我出错的地方。非常感谢任何帮助,谢谢!

我假设你的关联都在你的 Artwork 模型中列出了复数名称?它需要与您的索引定义相同。

至于字段与属性,一个好的经验法则是您希望用户输入并获得结果的任何内容都应该是一个字段。所以,我猜你想要艺术家的名字和姓氏作为字段。

因此,修改后的索引定义:

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

如果您仍然遇到错误,可以 运行 rake ts:index --trace 并与我们分享回溯吗? :)