如何解决当我使用回形针迁移错误时发生
运行这个命令=>
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。