问题描述
人们如何编写涉及 Paperclip 的 Rails 迁移?我觉得我可能会遗漏一些明显的东西,因为我现在已经编写了自己的迁移助手 hacks,这使得它更容易并且还负责进行必要的文件系统更改.当然,在部署到生产环境之前,您应该在开发(和暂存)环境中测试运行这些类型的迁移.
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 迁移重命名、添加和删除助手
Paperclip 更改路径迁移助手(不是真正的数据库迁移,但认为它无论如何都适合)
Paperclip migration rename, add and remove helpers
Paperclip change path migration helper (not really a database migration but think it fits quite nice anyway)
是否有更好的解决方案或最佳做法?有些人似乎创建了rake任务等,感觉很麻烦.
Are there any better solutions or best practices? some people seems to create rake tasks etc. which feels quite cumbersome.
推荐答案
gem 中为此包含了生成器:
There are generators included in the gem for this:
轨道 2:
script/generate paperclip Class attachment1 (attachment2 ...)
Rails 3:
rails generate paperclip Class attachment1 (attachment2 ...)
例如
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
另请参阅自述文件
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
这篇关于如何进行涉及 Paperclip 的 Rails 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!