当我使用回形针迁移错误时发生

问题描述

运行这个命令=> rails generate paperclip photo image

创建迁移文件

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

迁移错误

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)

解决方法

我遇到了同样的问题,我通过如下更改迁移来解决它,

class AddAttachmentImageToPhotos < ActiveRecord::Migration[5.1]

  def self.up
    add_attachment :photos,:image
  end

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

在使用使用 PaperClip 的 Ruby 3 从 Rails 5 升级到 6.1 时,我遇到了类似的问题。不确定是什么导致了问题,但像正常迁移一样添加列有帮助。

  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,:logo_updated_at,:datetime
    end

    def down
       remove_column :users,:string
       remove_column :users,:integer
       remove_column :users,:datetime
    end
  end

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...