Skip to main content

How to Migrate from Paperclip to Rails ActiveStorage Discussion

General • Asked by Chris Oliver
This should return the path to file in this format: /users/avatar/xxx/xx/original/filename
Also, in my case i had to use something like:
file_path = k.send(value).path.split('/system', 2).second
because it exists locally and path had system as a prefix.

For Digital Ocean Spaces, could you check the ETag of an object for the checksum rather than downloading the entire thing? Their docs say the ETag is "an MD5 hash of the object".

When I run the migration (with the provided code) I get:

ArgumentError: wrong number of arguments (given 1, expected 2..3)
/Users/nicknoble/products/Lnky/db/migrate/20180531013954_convert_to_active_storage.rb:14:in prepare'

I fixed this by changing to:

active_storage_blob_statement = ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL)
      INSERT INTO active_storage_blobs (
        `key`, filename, content_type, metadata, byte_size, checksum, created_at
      ) VALUES (?, ?, ?, '{}', ?, ?, ?)

    active_storage_attachment_statement = ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL)
      INSERT INTO active_storage_attachments (
        name, record_type, record_id, blob_id, created_at
      ) VALUES (?, ?, ?, #{get_blob_id}, ?)

When running migration:

ArgumentError: You tried to define an enum named "industry" on the model "Company", but this will generate a instance method "horeca?", which is already defined by another enum.  

I think this is because of the first line, when we require all the models. I just commented that line out and worked without that kind of error.

When can we actually remove paperclip?

Instead of building the URL in the def key method, can't I use the same thing I used in checksum?
url = attachment.url
I'm attempting to modify this migration to transition from Shrine to Active Storage.

get_blob_id = LASTVAL() was returning the id from the attachments table, NOT the blob_id. I had to change it to get_blob_id = "(SELECT max(id) from active_storage_blobs)". Any thoughts on this?

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.