我的问题与此问题基本相同: 在同一模型上具有多个关联的多态关联
但是,提议/接受的解决方案不起作用,如后面的评论者所示.
我有一个Photo类,在我的应用程序中使用.帖子可以有一张照片.但是,我想重新使用多态关系来添加辅助照片.
之前:
class Photo
belongs_to :attachable, :polymorphic => true
end
class Post
has_one :photo, :as => :attachable, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
期望:
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
Run Code Online (Sandbox Code Playgroud)
但是,由于无法找到"SecondaryPhoto"类,因此失败.基于我从其他线程可以看出的内容,我想做:
has_one :secondary_photo, :as => :attachable, :class_name => "Photo", :dependent => :destroy
Run Code Online (Sandbox Code Playgroud)
除了调用Post#secondary_photo之外,只返回通过Photo Association附加的相同照片,例如Post#photo === Post#secondary_photo.看看SQL,它确实WHERE type ="Photo"而不是像我想要的那样"SecondaryPhoto"......
思考?谢谢!