pjmanning
pjmanning

Reputation: 1311

Rails Reset seeds.rb

I've created a dropdown menu database of categories in my seeds.rb file and accidentally did rake db:seed another time to add duplicate categories. Being a noob, I'd like to know what's the best way in a development ENV to make the category dropdown reset and put back the following categories...

seeds.rb

Category.create(name: 'General')
Category.create(name: 'Birthday')
Category.create(name: 'Sports and Recreation')
Category.create(name: 'Music')
Category.create(name: 'Nature')
Category.create(name: 'Education')
Category.create(name: 'Political')

Upvotes: 1

Views: 2721

Answers (2)

Andrew Ferk
Andrew Ferk

Reputation: 3728

You can prepend your seeds.rb file with Category.destroy_all. This will instantiate and destroy all your categories.

Upvotes: 2

BlueFish
BlueFish

Reputation: 5145

If you didn't want duplicates, you can fall back to:

Category.find_or_create_by_name('General')

This will attempt to find it (by name), or create the record if it doesn't exist.

Upvotes: 0

Related Questions