How to remove a column from my Rails model?

前端 未结 3 568
温柔的废话
温柔的废话 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:46

    If you know the columns you want to remove you can use the convention: Remove..From.. when naming your migrations. Additionally you can include the column names when running the migration command.

    The form of the command:

    rails g migration Remove..From.. col1:type col2:type col3:type
    

    For example:

    rails g migration RemoveProjectIDFromProjects project_id:string
    

    generates the following migration file:

    class RemoveProjectIdFromProjects < ActiveRecord::Migration
      def self.up
        remove_column :projects, :project_id
      end
    
      def self.down
        add_column :projects, :project_id, :string
      end
    end
    

提交回复
热议问题