思考狮身人面像 - 未定义的方法`klass'

时间:2015-07-18 05:32:43

标签: ruby-on-rails indexing associations rake thinking-sphinx

我有一个Artwork模型,该模型与HABTM的模型SubjectLocationKeywordStyleMedium相关联。我还与一对多的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

我无法弄清楚为什么我会一直遇到不同的错误。我可能没有牢牢掌握字段与属性,也许这就是我出错的地方。非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

我假设您的关联在您的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并与我们分享回溯吗? :)