Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Rails migration involving Paperclip

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 makes it easier and also take care of doing necessary filesystem changes. And of course you should test run these kinds of migrations in a development (and staging) environment before deploying to production.

Paperclip migration rename, add and remove helpers
Paperclip change path migration helper (not really a database migration but think it fits quite nice anyway)

Are there any better solutions or best practices? some people seems to create rake tasks etc. which feels quite cumbersome.

like image 231
Mattias Wadman Avatar asked Oct 19 '11 19:10

Mattias Wadman


1 Answers

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
like image 51
Jared Avatar answered Sep 29 '22 11:09

Jared