开发者

Ruby on Rails: How and where should I add persistent data to my db?

开发者 https://www.devze.com 2023-02-17 18:00 出处:网络
I\'m fairly new to RoR and I\'m developing some basic role-based permissions model for users. However, I want some roles to be in the database by default, where do I insert the respective rows into m

I'm fairly new to RoR and I'm developing some basic role-based permissions model for users.

However, I want some roles to be in the database by default, where do I insert the respective rows into my db?

So far, I'm doing it in a separate migration, that simply 'create!'s the respective objects.

However if I run rake db:migrate && rake db:test:prepare, only my development db gets populated with the data, while the test-db only updates its sch开发者_如何学Goema and stays empty.

So, how/where do I insert such default data into my databases?

thx for any help


You can find my answer to a similar question here: prepopulating admin user in database with authlogic rails plugin

Basically rails has a file called db/seed.rb where you can put what you need to add by default and then run "rake db:seed" to load it.


I have found a good gem that is interesting for handling such scenarios which is seed-fu.


I would use migrations for data that shapes database - that is basic part of application. But db/seed.rb (rake db:seed) is also a good way - if you do not expect big changes after going to production.


No way is perfect, data in migrations is not stored to schema for restoring test database, but calling two times the same seed.rb is also not good. Don't forget to make the logic intelligent enough to not conflict when rerun/


I have used seed-fu, very easy to use and will do exactly what you need.

0

精彩评论

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