How do I add some inserts in rails migration?

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)"

Leave a Comment