Linus Oleander
Linus Oleander

Reputation: 18127

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

Person two

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

Answers (1)

Paul Sturgess
Paul Sturgess

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

Related Questions