Paperclip gem Rails [6.1] : migration error wrong number of arguments (given 3, expected 2)

2.3k Views Asked by At

Im trying to install paperclip gem on my rails app and I had 3 problems in succession and I would like to point out to them, it may be related to the last problem :

1- I coulden't install the paperclip dependency "mimemagic" => I solved it by adding the file freedesktop.org.xml.in and its variable FREEDESKTOP_MIME_TYPES_PATH to my windows machine.

2 - Then after initializing paperclip I couldn't do the migration => I solved it by adding the version of my rails app which is [6.1] bessid the Active record so it become class AddAttachmentImageToPics < ActiveRecord::Migration[6.1].

3- The last problem which Im stuck in is when I type rails db:migrate I get this message that says :

ArgumentError: wrong number of arguments (given 3, expected 2)

This is the content of my migration file :

class AddAttachmentImageToPics < ActiveRecord::Migration[6.1]
  def self.up
    change_table :pics do |t|
      t.attachment :image
    end
  end

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

the message in cmd :

C:\Users\Admin\Desktop\Ruby\instagrameme>rails db:migrate
== 20220205102326 AddAttachmentImageToPics: migrating =========================
-- change_table(:pics)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (given 3, expected 2)
C:/Users/Admin/Desktop/Ruby/instagrameme/db/migrate/20220205102326_add_attachment_image_to_pics.rb:4:in `block in up'
C:/Users/Admin/Desktop/Ruby/instagrameme/db/migrate/20220205102326_add_attachment_image_to_pics.rb:3:in `up'

Caused by:
ArgumentError: wrong number of arguments (given 3, expected 2)
C:/Users/Admin/Desktop/Ruby/instagrameme/db/migrate/20220205102326_add_attachment_image_to_pics.rb:4:in `block in up'
C:/Users/Admin/Desktop/Ruby/instagrameme/db/migrate/20220205102326_add_attachment_image_to_pics.rb:3:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
1

There are 1 best solutions below

0
Cameron On

I think this is the root cause of your issue. The column method used took 3 arguments in rails 5, but the 3rd argument got changed to a keyword argument in rails 6. This is an issue for ruby >= 2.7 because they removed automatic conversion of a hash to keyword arguments.

You would probably need to fork or monkey patch paperclip to fix this issue.