2

In my app I have a table for ROLES. Pretty simple: 1: Admin, DESC stuff 2: Guest, Desc stuff

etc..

The issue I just got hit with was I went to deploy the app on heroku and everything broke, reason was that these default roles in the database were not populated on deployment... Something I hadn't thought about.

With Rails 3, is there a way to say, hello Mr Rails, here are the table's default values? on migrate or database create?

Thanks

TheExit
  • 5,220
  • 12
  • 37
  • 49

2 Answers2

6

There is a concept called Seed Data in Rails which you can use to do this.

There is a file called seeds.rb is created in the db directory. In which you can create such things.

So for example

 Role.create(:name => "Administrator") 

will go into this file.

You can call rake db:seed to seed this data into your application.

There is a railscast about this as well - http://railscasts.com/episodes/179-seed-data.

Rishav Rastogi
  • 15,484
  • 3
  • 42
  • 47
0

If the roles are part for your application logic it might be better to define them as an array in your codebase. This also prevents having to sync the databases if new roles are added.

Petrik de Heus
  • 970
  • 6
  • 9