Rails多态has_many:通过
我从外部API中提取一些数据,并希望在本地缓存结果。 我有一个class SearchTerm
,我希望通过表searchable_items
与几个不同的ActiveRecord类型相关联。 我很确定我的表格设置正确,但我的关联中的某些内容一定是错的。
class Foo :searchable, :through => :searchable_items end class Bar :searchable, :through => :searchable_items end class SearchTerm :searchable_items end class SearchableItem true end
我希望能够做一些像SearchTerm.find_by_term('SearchTerm').searchables
(它将返回一个Foo和Bar对象的数组)然而,我得到错误Could not find the association :searchable_items in model SearchTerm
提前感谢您提供给我的任何见解!
您需要将has_many :searchable_items
关联添加到Foo
, Bar
和SearchTerm
模型,因为:through => :searchable_items
选项指的是该关联。
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association