17

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.

1 Answer 1

38

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
3
  • Thanks, I should have been a bit more clear that I mostly meant migrations involving existing paperclip columns and doing rename and change of path which will need filesystem changes etc. Commented Dec 24, 2011 at 2:00
  • 1
    Ah, check out this question then: stackoverflow.com/questions/2708115/…
    – Jared
    Commented Dec 29, 2011 at 0:47
  • Ok, That locka more or less as what i do in my helpers. Guess thats how people do it then. Commented Dec 29, 2011 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.