Ruby-on-rails 如何进行涉及 Paperclip 的 Rails 迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7827141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do Rails migration involving Paperclip
提问by Mattias Wadman
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的 Rails 迁移?我觉得我可能会遗漏一些明显的东西,因为我现在已经编写了我自己的迁移助手 hacks,这使得它更容易并且还负责进行必要的文件系统更改。当然,在部署到生产环境之前,您应该在开发(和暂存)环境中测试运行这些类型的迁移。
Paperclip migration rename, add and remove helpers
Paperclip change path migration helper(not really a database migration but think it fits quite nice anyway)
Paperclip 迁移重命名、添加和删除助手
Paperclip 更改路径迁移助手(不是真正的数据库迁移,但认为它无论如何都非常适合)
Are there any better solutions or best practices? some people seems to create rake tasks etc. which feels quite cumbersome.
有没有更好的解决方案或最佳实践?有些人似乎创建了 rake 任务等,这感觉很麻烦。
回答by Jared
There are generators included in the gem for this:
为此,gem 中包含生成器:
Rails 2:
轨道 2:
script/generate paperclip Class attachment1 (attachment2 ...)
Rails 3:
轨道 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

