How to remove a column from my Rails model?

前端 未结 3 563
温柔的废话
温柔的废话 2021-01-30 16:27

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 s

3条回答
  •  清酒与你
    2021-01-30 16:36

    To remove a database column, you have to generate a migration:

    script/rails g migration RemoveColumns
    

    Then in the self.up class method, remove your columns:

    def self.up
      remove_column :table_name, :column_name
    end
    

    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
    

    The Rails Guide for this goes into much more detail.

提交回复
热议问题