在 active_storage_attachments 上设置 record_type 的 ActiveStorage 问题 [英] ActiveStorage issue to set record_type on active_storage_attachments

查看:38
本文介绍了在 active_storage_attachments 上设置 record_type 的 ActiveStorage 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ActiveStorage 有问题

案例是我有一个名为 Setting::Profile 的模型和 Admin 表和附件的名称 :avatar上传过程成功,但无法找到何时获取图片,因为在表 active_storage_attachments 中的属性 record_type 存储名称为 Setting::Profile, 假设名称为 Admin

如何添加一行来为record_type 属性做准备?谢谢

这是我的演示:(比如User.last.avatar.class) 但ActiveStorage::Attachment.

所以,你不能调用例如 .attached? 方法就可以了.您必须使用 profile.avatar.present? 来检查头像是否存在.

一个可能更好的解决方案是以这种方式定义实例方法头像:

定义头像ActiveStorage::Attached::One.new('avatar', User.find(id),dependent: :purge_later)结尾

需要实例化一个ActiveStorage::Attached::One的对象,但记录必须是User类(匹配record_type),这就是 User.find(id) 的原因.现在所有方法都可用:profile.avatar.methods.

I have a problem for ActiveStorage

the case is I have a model named Setting::Profile and name of Admin table and attachment :avatar for uploading process succeed, but when to get the picture can not be found because at table active_storage_attachments in attribute record_type stored with name Setting::Profile, supposed with name Admin

How to add a line to prepare for the record_type attribute? Thanks

This my demo: https://github.com/SunDi3yansyah-forks/AppActiveStorage

解决方案

I found this problem (try in console).

If you set profile = Account::Profile.last then call profile.avatar.attached? it returns false. This is because the column record_type in ActiveStorage::Attachment is set to User.

So, you can not access the blob because profile.avatar.blob returns the following query:

SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 1], ["record_type", "Account::Profile"], ["name", "avatar"], ["LIMIT", 1]]

And the error: Module::DelegationError: blob delegated to attachment, but attachment is nil

One possible workaround that I found is to define Account::Profile as follows:

class Account::Profile < ApplicationRecord
    self.table_name = "users"
    # has_one_attached :avatar # remove this

  def avatar
    ActiveStorage::Attachment.where(name: :avatar, record_type: 'User', record_id: self.id).last
  end

end

This works for showing the image but has the problem that profile.avatar.class is not ActiveStorage::Attached::One (like User.last.avatar.class) but ActiveStorage::Attachment.

So, you can not call for example .attached? method on it. You must use profile.avatar.present? to check if the avatar is present.

A possible better solution is to define the instance method avatar in this way:

def avatar
  ActiveStorage::Attached::One.new('avatar', User.find(id), dependent: :purge_later)
end

It is required to instantiate an object of ActiveStorage::Attached::One but the record must be User class (to match record_type), that's why User.find(id). Now all methods are available: profile.avatar.methods.

这篇关于在 active_storage_attachments 上设置 record_type 的 ActiveStorage 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆