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

60 Views Asked by At

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

There are 1 best solutions below

0
smathy On BEST ANSWER

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