How to do Rails migration involving Paperclip

╄→гoц情女王★ 提交于 2019-12-03 06:07:57

There are generators included in the gem for this:

Rails 2:

script/generate paperclip Class attachment1 (attachment2 ...)

Rails 3:

rails generate paperclip Class attachment1 (attachment2 ...) 

e.g.

rails generate paperclip User avatar 

generates:

class AddAttachmentsAvatarToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :avatar_file_name, :string
    add_column :users, :avatar_content_type, :string
    add_column :users, :avatar_file_size, :integer
    add_column :users, :avatar_updated_at, :datetime
  end

  def self.down
    remove_column :users, :avatar_file_name
    remove_column :users, :avatar_content_type
    remove_column :users, :avatar_file_size
    remove_column :users, :avatar_updated_at
  end
end

Also see the helper methods used in the example in the readme

class AddAvatarColumnsToUser < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.has_attached_file :avatar
    end
  end

  def self.down
    drop_attached_file :users, :avatar
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!