开发者

Rails - Auto Populate Data in a Table on Deployment - Roles

开发者 https://www.devze.com 2023-01-23 05:39 出处:网络
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 w

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 开发者_Go百科values? on migrate or database create?

Thanks


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消