2011-01-09 122 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

預先感謝任何見解你可以提供給我!

回答