如何从我的 Rails 模型中删除一列? [英] How to remove a column from my Rails model?
本文介绍了如何从我的 Rails 模型中删除一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从我已经创建的 rails 模型中删除几列,并且在该模型中有一些行条目.怎么做?任何包含在 rails 中修改架构的详细信息的链接?我使用的是 Rails 版本 3.
I need to remove a few columns from my rails model which i already created and have some row entries in that model. How to do it? Any links which has details for modifying the schema in rails ? I'm using rails version 3.
推荐答案
要删除数据库列,您必须生成迁移:
To remove a database column, you have to generate a migration:
script/rails g migration RemoveColumns
然后在 self.up 类方法中,删除您的列:
Then in the self.up class method, remove your columns:
def self.up
remove_column :table_name, :column_name
end
您可能还想将它们重新添加到 self.down 类方法中:
You may want to add them back in the self.down class method as well:
def self.down
add_column :table_name, :column_name, :type
end
Rails 指南对此进行了更详细的介绍.
The Rails Guide for this goes into much more detail.
这篇关于如何从我的 Rails 模型中删除一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文