我的问题基本上和这个一样:
然而,正如后面的一位 comments 者所说明的,提议/接受的解决方案不起作用.
我有一个照片类,它在我的应用程序中随处可见.一篇帖子可以有一张照片.然而,我想重新使用多态关系来添加第二张照片.
之前:
class Photo
belongs_to :attachable, :polymorphic => true
end
class Post
has_one :photo, :as => :attachable, :dependent => :destroy
end
渴望的:
class Photo
belongs_to :attachable, :polymorphic => true
end
class Post
has_one :photo, :as => :attachable, :dependent => :destroy
has_one :secondary_photo, :as => :attachable, :dependent => :destroy
end
然而,这失败了,因为它找不到类"SecondaryPhoto".根据我从另一条线索中可以看出的情况,我想做:
has_one :secondary_photo, :as => :attachable, :class_name => "Photo", :dependent => :destroy
除了调用Post#secondary _photo只返回通过照片关联附加的相同照片,例如Post#photo==Post#secondary _photo.看看SQL,它会在WHERE type="Photo"中执行,而不是像我希望的那样,比如说"SecondaryPhoto"...
思想?谢谢