2011-01-09 92 views
14

我拉从外部API的一些数据,并希望本地缓存结果。我有一个class SearchTerm,我想通过表searchable_items与几个不同的ActiveRecord类型关联。我很确定我的表格设置正确,但是我的关联中的某些内容一定是错误的。Rails多态has_many:通过

class Foo < ActiveRecord::Base 
    has_many :search_terms, :as => :searchable, :through => :searchable_items 
end 

class Bar < ActiveRecord::Base 
    has_many :search_terms, :as => :searchable, :through => :searchable_items 
end 

class SearchTerm < ActiveRecord::Base 
    has_many :searchables, :through => :searchable_items 
end 

class SearchableItem < ActiveRecord::Base 
    belongs_to :search_term 
    belongs_to :searchable, :polymorphic => true 
end 

我希望能够像做SearchTerm.find_by_term('SearchTerm').searchables(它会返回Foo和Bar对象的数组),但是,我得到的错误 Could not find the association :searchable_items in model SearchTerm

预先感谢任何见解你可以提供给我!

回答