Rails Migration Complains About Undefined Method 'Attachment' Using Paperclip

Rails migration complains about undefined method `attachment' using paperclip

Ok, figured it out. The add_attachment helper is defined in the schema.rb file. Don't know if it's right or not but if I include:

include Paperclip::Schema

into the file, it works.

Rails migrations complains about add_attachment

paperclip add_attachment creates four fields for each attachment, so to fix that, you can remove the paperclip gem, remove the old migration, and create a new one removing the fields added by add_attachment

"add_attachment :users, :photo" creates four fields in the users table
photo_file_name
photo_content_type
photo_file_size
photo_uploaded_at

after removing the old migration that uses remove_attachment, you can create a new one with:

remove_column :users, :photo_file_name
remove_column :users, :photo_content_type
remove_column :users, :photo_file_size
remove_column :users, :photo_file_name

Paperclip::Attachment - undefined method `attach'. Asset Migration Rake Task

I think that the solution is in the documentation you have shared.

class Organization < ApplicationRecord
# New ActiveStorage declaration
has_one_attached :logo

# Old Paperclip config
# must be removed BEFORE to running the rake task so that
# all of the new ActiveStorage goodness can be used when
# calling organization.logo
has_attached_file :logo,
path: "/organizations/:id/:basename_:style.:extension",
default_url: "https://s3.amazonaws.com/xxxxx/organizations/missing_:style.jpg",
default_style: :normal,
styles: { thumb: "64x64#", normal: "400x400>" },
convert_options: { thumb: "-quality 100 -strip", normal: "-quality 75 -strip" }
end

It looks like the has_attached_file has to be replaced by has_one_attached.

Otherwise, image will use Paperclip instead of ActiveStorage (which has attach method).

Rails Paperclip how to rollback only one attachment

Would something like this work for you? Run a new migration that removes the avatar.

class RemoveAvatarColumnsToUsers < ActiveRecord::Migration
def self.up
remove_attachment :users, :avatar
end

def self.down
add_attachment :users, :avatar
end
end

Hope this helps

undefined method `execute_prepared' Rails Paperclip to ActiveStorage migration

I faced the same issue when running the migration. I had to change the two execute_prepared statements to use exec_prepared instead:

ActiveRecord::Base.connection.raw_connection.exec_prepared(
'active_storage_blob_statement', [
...
])

ActiveRecord::Base.connection.raw_connection.exec_prepared(
'active_storage_attachment_statement', [
...
])

I don't quite remember why, but I also had to pass the ID of the last blob record inserted to the attachment insert statement. You can skip this if you don't need this:

active_storage_attachment_statement = ActiveRecord::Base.connection.raw_connection.prepare('active_storage_attachment_statement', <<-SQL)
INSERT INTO active_storage_attachments (
name, record_type, record_id, blob_id, created_at
) VALUES ($1, $2, $3, #{"(SELECT max(id) from active_storage_blobs)"}, $4)
SQL

Here is my complete migration.

Why is db:migrate failing when i try to add attachment fields for paperclip?

The migration that was created for me doesn't use the t.has_attached_file terminology anymore, it actually adds the columns explicitly. The migration would be created by running:

rails generate paperclip Answer diagram

Check out the example here.

Rails and paperclip, delete the record but don't delete the attachment

you may want to take a look at how Attachment#assign (called when you do object.attachment = new_attachment) is implemented in paperclip.
Basically, it makes a bit of setup, then calls Attachment#clear, then it saves the new file.

Attachment#clear puts the old file in a deletion queue that is processed when you call save again, what you want is simply to avoid the call to clear, which you could do by either writing a new assign method which skips that line or by monkey patching #clear so that it becomes a no-op. In theory you could just monkey patch it on the instances where you want this to happen, but it seems to me you may want to do it for the whole project.

Or you can clear the instance variable holding the processing queue. That variable does not have an accessor, but it should be trivial to do an instance_variable_get



Related Topics



Leave a reply



Submit