如何添加喜欢和评论ActiveStorage blob

时间:2018-05-28 16:52:19

标签: ruby-on-rails ruby rails-activestorage

标题说明了一切。如何定义与我的模型相关联的ActiveStorage::Attachment的关系?我希望commentreact能够photos以及Post

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, as: :commentable
  has_many :reactions, as: :reactionable

  has_many_attached :uploads
end

1 个答案:

答案 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