Rails: Modifying a Model Generated by Scaffolding

前端 未结 3 1405
逝去的感伤
逝去的感伤 2020-12-24 02:10

How do you modify a model you\'ve generated using modeling? For example, the model myModel originally had columns a, b and c, but I now want to add column d.

3条回答
  •  没有蜡笔的小新
    2020-12-24 02:55

    The best answer I've found so far is run this from your project root:

    ruby script/generate migration add_d_column_to_myModel 
    

    Then edit the new migration file located in db/migration to look something like:

      def self.up
        add_column :myModel, :d, :string
      end
    
      def self.down
        remove_column :myModel, :d
      end
    

    The last step will be to update your views accordingly.

    Answer found here

    Table functions found here

提交回复
热议问题