How to do Rails migration involving Paperclip

后端 未结 1 1555
星月不相逢
星月不相逢 2021-02-07 09:51

How do people write their Rails migrations that involve Paperclip? I feel that I might be missing something obvious as I have now written my own migration helpers hacks that mak

1条回答
  •  孤城傲影
    2021-02-07 10:10

    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
    

    0 讨论(0)
提交回复
热议问题