Best practice when using seed.rb
I'm having some difficulties understanding how to use the seed.rb
script in rails.
So far, I've used it to populate my database every time i deploy my application.
Like this.
seed.rb
["Video", "Tv"].each do |thing|
Category.create(name: thing)
end
category.rb
class Category < ActiveRecord::Base
validates_uniqueness_of :name
end
The script can now be runned every deploy or pull. Anyone in the dev team can now add their own category without have to worry about duplications.
Like this.
Person one
- Adding the
Table
category toseed.rb
. - Commit and push to master.
Person two
- Pull master.
- Run
rake db:migrate
andrake db:seed
to ensure that the local database is up-to-date. - Deploy application to production server.
rake db:seed
is being runned on the server to ensure an up-to-date database.
Is this wor开发者_如何学Pythonkflow okay, if not, where should I put the new data to ensure that every developer has an up-to-date database?
I would recommend writing your seed so that it can be run more than once without trying to create duplicate categories...
["Video", "Tv"].each do |thing|
Category.find_or_create_by_name(thing)
end
精彩评论