标题说明了一切。如何定义与我的模型相关联的ActiveStorage::Attachment
的关系?我希望comment
和react
能够photos
以及Post
。
class Post < ApplicationRecord
belongs_to :user
has_many :comments, as: :commentable
has_many :reactions, as: :reactionable
has_many_attached :uploads
end
答案 0 :(得分:3)
至少你可以通过引入一个中间模型(这将是喜欢/反应的目标并将主持上传)来做到这一点。
class Post
has_many :likable_photos
end
class LikablePhoto
has_many :likes, as: :likeable # or however else you decide to store likes
has_many :reactions, as: :reactionable
has_one_attached :upload
end