rizwan ali
rizwan ali

Reputation: 11

when I use paperclip migration error occur

run this command => rails generate paperclip photo image

create migration file

class AddAttachmentImageToPhotos < ActiveRecord::Migration[5.1]

  def self.up
    change_table :photos do |t|

      t.attachment :image
    end
  end

  def self.down
    remove_attachment :photos, :image
  end
end

migration error

rails db:migrate
== 20210223102018 AddAttachmentImageToPhotos: migrating =======================
-- change_table(:photos)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (given 3, expected 2)
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:4:in `block in up'
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:3:in `up'
/home/rizwan/projects/ClipUploader/bin/rails:5:in `<top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:10:in `block in <top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:7:in `<top (required)>'

Caused by:
ArgumentError: wrong number of arguments (given 3, expected 2)
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:4:in `block in up'
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:3:in `up'
/home/rizwan/projects/ClipUploader/bin/rails:5:in `<top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:10:in `block in <top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Upvotes: 0

Views: 1138

Answers (3)

Aftab Iqbal
Aftab Iqbal

Reputation: 1

You may use https://github.com/kreeti/kt-paperclip for storing images. Works perfectly for me. I was also stuck in the same error but this is working fine now.

Upvotes: -1

Prem Anand
Prem Anand

Reputation: 1496

I had a similar issue when upgrading from Rails 5 to 6.1 with Ruby 3 which was using PaperClip. Not sure what was causing the issue but adding the columns like a normal migration helped.

  class AddLogoColumnsToUsers < ActiveRecord::Migration[6.1]
    def up
       add_column :users, :logo_file_name, :string
       add_column :users, :logo_file_size, :integer
       add_column :users, :logo_content_type, :string
       add_column :users, :logo_updated_at, :datetime
    end

    def down
       remove_column :users, :logo_file_name, :string
       remove_column :users, :logo_file_size, :integer
       remove_column :users, :logo_content_type, :string
       remove_column :users, :logo_updated_at, :datetime
    end
  end

Upvotes: 4

Aditya Tiwari
Aditya Tiwari

Reputation: 209

I had the same issue, I solved it by changing the migration as follows,

class AddAttachmentImageToPhotos < ActiveRecord::Migration[5.1]

  def self.up
    add_attachment :photos, :image
  end

  def self.down
    remove_attachment :photos, :image
  end
end

Upvotes: 1

Related Questions