如何进行涉及Paperclip的Rails迁移

时间:2011-10-19 19:41:25

标签: ruby-on-rails paperclip rails-migrations

人们如何撰写涉及Paperclip的Rails迁移?我觉得我可能会遗漏一些显而易见的东西,因为我现在已经编写了自己的迁移助手黑客,这使得它变得更容易并且还可以处理必要的文件系统更改。当然,在部署到生产环境之前,您应该在开发(和临时)环境中测试运行这些类型的迁移。

Paperclip migration rename, add and remove helpers
Paperclip change path migration helper(不是真正的数据库迁移,但认为它非常适合)

有没有更好的解决方案或最佳做法?有些人似乎创造了耙子任务等,感觉非常麻烦。

1 个答案:

答案 0 :(得分:37)

宝石中包含了生成器:

Rails 2:

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

Rails 3:

rails generate paperclip Class attachment1 (attachment2 ...) 

e.g。

rails generate paperclip User avatar 

产生

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

另请参阅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