Add Rows on Migrations

前端 未结 5 1804
故里飘歌
故里飘歌 2020-12-15 18:41

I\'d like to know which is the preferred way to add records to a database table in a Rails Migration. I\'ve read on Ola Bini\'s book (Jruby on Rails) that he does something

5条回答
  •  时光说笑
    2020-12-15 19:05

    your migrations have access to all your models, so you shouldn't be creating a class inside the migration.

    I am using the latest rails, and I can confirm that the example you posted definitely OUGHT to work.

    However, migrations are a special beast. As long as you are clear, I don't see anything wrong with an ActiveRecord::Base.connection.execute("INSERT INTO product_types (name) VALUES ('type1'), ('type2')").

    The advantage to this is, you can easily generate it by using some kind of GUI or web front-end to populate your starting data, and then doing a mysqldump -uroot database_name.product_types.

    Whatever makes things easiest for the kind of person who's going to be executing your migrations and maintaining the product.

提交回复
热议问题