I have seeded a row of data to my table by editing db/seed.rb
file and executing rake db:seed
command. Unknowingly, I put some wrong information in to that row. So I want to remove the previously added row of data. Is there any rake command for the same like rake db:rollback
for rake db:migrate
.

- 42,737
- 46
- 157
- 243

- 6,788
- 7
- 47
- 85
-
2Drop the database, recreate and seed again. :) – Sergio Tulentsev Feb 19 '13 at 10:21
-
@Sergio Tulentsev: I agree. But I have to keep the data which was already there before I did the last seed. I only want to remove the last seeded data. Can you please help me to do that. – Rajesh Omanakuttan Feb 19 '13 at 10:26
-
2There's no rake task for that. Find and delete the data manually. If you anticipate it to happen again, write a script. – Sergio Tulentsev Feb 19 '13 at 10:41
-
2that is a one of the reasons why I dont use seed, always use a migration script to populate your data – sameera207 Feb 19 '13 at 10:42
2 Answers
There are a couple of aspects to this:
1: You want to change the seed data when no other data is present in the database:
You should simply redo the rake db:seed
after updating the seed.rb file. Make sure you have MyModel.delete_all
before you try to add anything to that model.
2: You want to change the seed data, but there are other data added to the database
This is a bit harder. Often the simplest thing to do here is to manually change the data with either raw sql-statements, or with the use of tools like PhpPpAdmin, PhpMyAdmin etc.
Now, there is possiby one way to hack this together, and that would be to do some voodoo in the seed.rb file. So you could run rake db:seed deseed=true
, then in your seed.rb:
if ENV['deseed']
#Do your deseeding action here
else
#Do your seeding here.
end
You could even get real crazy and do something like this:
deseed = ENV['desee']
#DANGER: Dirty hacks upcoming:
deseed? myModelCall = MyModel.method(:destroy_all): myModelCall = MyModel.method(:create)
myModelCall.call :user_id_or_whatevs => 23 #this creates or deletes a MyModel entity with the given parameters
#NOTE this might not work in all cases and I would not necessarily recommend doing this.
#<3uby

- 12,420
- 9
- 82
- 110
I had similar issues when I seeded my data. In fact, I ran the seed command twice and I couldn't find a way to revoke the second seed. However, I had to run rails db:reset
command and then run the rails db:seed
command again and that fixed the problem for me.

- 45
- 1
- 8