Ruby on rails 如何销毁RubyonRails中由回形针错误创建的列
正如标题所示,我无意中在模型中添加了一些随机附件列。假设我没有Ruby on rails 如何销毁RubyonRails中由回形针错误创建的列,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,正如标题所示,我无意中在模型中添加了一些随机附件列。假设我没有rails生成回形针组合href 如何删除创建的列rails销毁回形针组合href似乎不起作用 通常对于运行的迁移rake db:rollback。因为这是一个生成器,所以您可以手动编辑表并删除列 alter table portfolio drop column href_content_type; alter table portfolio drop column href_file_name; -- ... etc for ea
rails生成回形针组合href
如何删除创建的列<代码>rails销毁回形针组合href似乎不起作用 通常对于运行的迁移
rake db:rollback
。因为这是一个生成器,所以您可以手动编辑表并删除列
alter table portfolio drop column href_content_type;
alter table portfolio drop column href_file_name;
-- ... etc for each of the Paperclip columns.
您也可以创建一个迁移来删除列,但是如果您的整个团队也运行生成器,或者您签入schema.rb和其他人也运行了生成器,那么这将是一个过度的操作
以下是用于删除列的迁移示例:
class RemoveMagazineFromBooks < ActiveRecord::Migration
def change
# This is not used ANYWHERE...
if column_exists? :refinery_books, :magazine
remove_column :refinery_books, :magazine
remove_index :refinery_books, :magazine if index_exists? :refinery_books, :magazine
end
end
end
class RemoveMagazineFromBooks
注意测试以检查列是否存在。有些人可能没有运行上一次迁移,也不会拥有该列。我已创建了一个迁移文件,并将内容更改为:
class RemoveAttachmentHrefToPortfolios < ActiveRecord::Migration
def self.up
remove_attachment :portfolios, :href
end
def self.down
remove_attachment :portfolios, :href
end
end
class RemoveAttachmentHrefToPortfolios
问题是这不是一种优雅而正确的方法。我希望有人能改进这个答案。
$rails生成迁移RemoveColumnsFromModel公文包href
应该进行迁移,从某些模型的表中删除两列“href”和“portfolio”。迁移将类似于:
class RemoveColumnsFromUser < ActiveRecord::Migration
def change
remove_column :users, :foo, :string
remove_column :users, :bar, :string
end
end
class RemoveColumnsFromUser
这来自回形针:
这将回滚对DB架构的最新更改:
rake db:rollback
之后,只需删除迁移文件
这是假定您没有将任何更改/迁移提交到共享存储库。对于最新版本的rails 4++
只需创建一个迁移
示例:
rails g迁移将附件照片删除到课程
然后打开迁移文件
class RemoveAttachmentPhotoToCourse < ActiveRecord::Migration
def change
remove_attachment :courses, :photo
end
end
其中:photo是要删除的回形针附件的名称
courses是表名的名称生成器是否创建迁移文件?确定。但是我不能通过使用
destory
对应项来恢复此功能。这是一个很好的解决方案!但我担心我可能需要一个使用迁移的解决方案。我想出了一个解决办法。这是非常愚蠢的,因为我不理解self.up
和self.down
。你介意给我一些建议吗?
class RemoveAttachmentPhotoToCourse < ActiveRecord::Migration
def change
remove_attachment :courses, :photo
end
end
rake db:migrate