rails – 如何指定kt-paperclip宝石应计算校验和的列

huangapple go评论63阅读模式
英文:

rails - how to specify the column to where kt-paperclip gem should calculate the checksum

问题

I'm using the has_attached_file from paperclip gem to calculate a checksum on the file's content. By default, this calculation will be applied to the column that has the name fingerprint on it but the problem is that I have two columns that must have "fingerprint" in the name. Is there a way to specify in which column Paperclip should calculate the checksum?

here's what I've tried

has_attached_file(
    :file,
{
    hash_digest: Digest::SHA256,
    hash_digest_column: :original_file_fingerprint
 }
)

but nothing gets updated.
Any ideas?
https://github.com/thoughtbot/paperclip#checksum--fingerprint

英文:

I'm using the has_attached_file from paperclip gem to calculate a checksum on the file's content. By default, this calculation will be applied to the column that has the name fingerprint on it but the problem is that I have two columns that must have "fingerprint" in the name.
Is there a way to specify in which column Paperclip should calculate the checksum?

here's what I've tried

has_attached_file(
    :file,
{
    hash_digest: Digest::SHA256,
    hash_digest_column: :original_file_fingerprint
 }
)

but nothing gets updated.
Any ideas?
https://github.com/thoughtbot/paperclip#checksum--fingerprint

答案1

得分: 0

首先,拥有以“_fingerprint”结尾的多个列没有问题,Paperclip只使用与附件同名的列,即对于has_attached_file :file,它将使用file_fingerprint列。

其次,Paperclip实际上并不关心列,它关心属性的获取器/设置器,因此,如果你必须遵循某个使用file_fingerprint来表示其他内容的遗留数据库模式,那么你可以重新定义属性方法以使用不同的列。

根据你是否仍然需要从Rails模型中访问(实际的)file_fingerprint,有两个选项。

如果你不需要访问它:

alias_attribute :file_fingerprint, :original_file_fingerprint

如果你需要访问它(这些不会更改/修改Rails数据库查询语法,比如createupdatewhere子句等):

def file_fingerprint
  self[:original_file_fingerprint]
end

def file_fingerprint= v
  self[:original_file_fingerprint] = v
end
英文:

So first, there's no problem having multiple columns ending in _fingerprint, Paperclip only uses the column named after the attachment, ie. file_fingerprint for has_attached_file :file.

Second, Paperclip doesn't actually care about columns, it cares about attribute getters/setters, so if you have to conform to some legacy database schema that uses file_fingerprint for something else, then you can always just redefine the attribute methods to use a different column.

There are two options depending on whether you still need access to (the actual) file_fingerprint from your Rails model.

If you don't need access to it:

alias_attribute :file_fingerprint, :original_file_fingerprint

If you do (these will not change/alter Rails DB query syntax, like create, update, where clauses etc):

def file_fingerprint
  self[:original_file_fingerprint]
end

def file_fingerprint= v
  self[:original_file_fingerprint] = v
end

huangapple
  • 本文由 发表于 2023年7月7日 08:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76633181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定