How do I add some inserts in rails migration?

后端 未结 5 1123
说谎
说谎 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:36

    create a new migration file like 047_add_rows_in_system_settings.rb

     class AddRowsInAddSystemSettings < ActiveRecord::Migration
            def self.up
              SystemSetting.create{:name => "name1", :label => "Use notice?", :value => 1}
              SystemSetting.create{:name => "name2", :label => "Use notice?", :value => 2}
             end
    
            def self.down
              SystemSetting.delete_all
            end
          end
    

    OR

    while creating table

    046_system_settings.rb

    class AddSystemSettings < ActiveRecord::Migration
        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
    
          SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
        end
    
        def self.down
          drop_table :system_settings
        end
      end
    

    Ref:- http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

    0 讨论(0)
  • 2020-12-05 00:37

    Use can use seed data, it's a great way for this ! http://railscasts.com/episodes/179-seed-data

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-05 00:44

    Don't. If you're looking for seed data, you should use db/seeds.rb and rake db:seed instead. More info in this Railscast.

    Side note: Always make sure that the code in db/seeds.rb is idempotent. i.e. It should always be safe to re-run your seeds.

    But, if you must insert or modify data inside a migration (there are legitimate use-cases for this), it's best to use SQL statements instead. Your model class isn't guaranteed to still be around in the same form in a future version of your application, and running the migrations from scratch in the future might yield errors if you reference the model class directly.

    execute "insert into system_settings (name, label, value) values ('notice', 'Use notice?', 1)"
    
    0 讨论(0)
  • 2020-12-05 00:52

    Edit: PLEASE NOTE - Posters above are correct, you should not populate the DB inside migrations. Don't use this to add new data, only to modify data as part of changing the schema.

    For many things, using raw SQL will be preferable, but if you need to insert data as part of a migration (for instance, doing data conversion when breaking out a table into multiple tables), and you want some default AR stuff like convenient DB-independent escaping, you can define a local version of the model class:

    class MyMigrationSucksALittle < ActiveRecord::Migration
      class MyModel < ActiveRecord::Base
        # empty guard class, guaranteed to have basic AR behavior
      end
    
      ### My Migration Stuff Here
      ### ...
    
    end
    

    Note that this works best for simple cases; since the new class is in a different namespace (MyMigrationSucksALittle::MyModel), polymorphic associations declared in the guard model won't work correctly.

    A somewhat more detailed overview of available options is located here: http://railsguides.net/2014/01/30/change-data-in-migrations-like-a-boss/

    0 讨论(0)
提交回复
热议问题