# Predefined records in migration?

**URL:** <https://www.ruby-forum.com/t/predefined-records-in-migration/130236>\
**Category:** Rails\
**Created:** [February 26, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/predefined-records-in-migration/130236 "2008-02-26T17:06:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 26, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/predefined-records-in-migration/130236/1 "2008-02-26T17:06:57Z")

</div>

What would be a good way to create some predefined instances in the  
database for a certain model? For example: categories, genres or user-  
roles? They are pretty fixed, although you might want to add or remove  
some at a later stage.

Is it a good idea to put something like this in the migration itself?

def self.up  
create\_table :roles do |t|  
…  
end

Role.create :name =\> ‘admin’  
Role.create :name =\> ‘moderator’  
end

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 26, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/predefined-records-in-migration/130236/2 "2008-02-26T17:25:14Z")

</div>

Hi, yep thats fine if you think they are quite static and not likely  
to change. Or you could add them as fixtures and use “rake  
db:fixtures:load” to load them into your database.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 26, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/predefined-records-in-migration/130236/3 "2008-02-26T17:43:39Z")

</div>

Both of those are bad ideas in my opinion.

Fixtures should be used for test data only. Migrations seem like a  
logical  
spot for defalt data, but they can become brittle if you try to place  
records into the database at that time, as future changes to code  
(validations, before\_ callbacks, etc) can make them fail, killing your  
remaining migrations. Even if it seems like you can get away with it, I  
wouldn’t.

Here’s the way I do it… by making my own rake task.

In your lib/tasks folder, create a file called import.rake

Place this code in the file

namespace :db do  
desc “import initial database contents”  
task :import =\> :environment do  
admin = User.create(:login =\> “admin”, :password =\>“1234”,  
:password\_confirmation=\>“1234”, :email=\>“removed\_email\_address@domain.invalid”)  
admin\_role = Role.create :name =\> ‘admin’  
moderator\_role = Role.create :name =\> ‘moderator’  
admin.roles \<\< admin\_role  
admin.roles \<\< moderator\_role  
end  
end

Save it, then run

rake db:import

or

rake RAILS\_ENV=“production” db:import

And you’re all set. Make that part of your deploy:cold step in  
capistrano 🙂

Good luck!
