views:

29

answers:

1

I am trying to setup seed data for my Rails app and am counting on the id of each model being a certain value. How can I tell the table to, after MyModel.destroy_all to start counting from 0 again instead of where it left off?

+1  A: 

The best way is probably to drop the whole table, recreate it and then seed it. You can do this with custom rake tasks.

Try something like this to drop your table:

desc "Drop MyModel table"
  task :drop_my_model => :environment do
    connection = ActiveRecord::Base.connection
    connection.execute("drop table my_models")
  end
end

then you can rake

$ rake drop_my_model
$ rake db:migrate:up VERSION=my_version
Yannis
I've just realised through this http://stackoverflow.com/q/1316889/328998 that rake db:migrate:redo VERSION=my_version could do the trick
Yannis