Reputation: 18127
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
Table
category to seed.rb
.Person two
rake db:migrate
and rake db:seed
to ensure that the local database is up-to-date.rake db:seed
is being runned on the server to ensure an up-to-date database.Is this workflow okay, if not, where should I put the new data to ensure that every developer has an up-to-date database?
Upvotes: 4
Views: 2706
Reputation: 3294
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
Upvotes: 5