Search code examples
ruby-on-railsmigration

How do I add some inserts in rails migration?


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

thanks


Solution

  • 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