How do I add some inserts in rails migration?

后端 未结 5 1133
说谎
说谎 2020-12-04 23:47

After creating a table (by migration), I want to insert some entries directly. How must I write a migration for this?

thanks

5条回答
  •  北海茫月
    2020-12-05 00:40

    Update: This is the right answer: https://stackoverflow.com/a/2667747/7852


    Here's an example from ruby on rails api:

     class AddSystemSettings < ActiveRecord::Migration
        # create the table
        def self.up
          create_table :system_settings do |t|
            t.string  :name
            t.string  :label
            t.text  :value
            t.string  :type
            t.integer  :position
          end
    
          # populate the table
          SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
        end
    
        def self.down
          drop_table :system_settings
        end
      end
    

提交回复
热议问题