I started recently using seed.rb which I find really cool but there’s
something I haven’t figured out yet. Seeding is apparently for seeding
data but now I’m kinda playing around with it and it keeps adding data
so I have lots of duplicate data. Is there any command that truncates
every table on the database (like iterating over every model and
Now I have to db:migrate:reset first in order to have clean tables.
I read your post this morning and thought, “WOW! Rails has defined an
approach for seeding data. That’s great! I’ll have to look into
that.” But I didn’t have an answer for you. Then later this morning,
I went looking for something I’d seen on Ryan B. excellent
Railscasts site (http://railscasts.com/) and found episode #179
staring me in the face. I highly recommend watching Ryan’s tutorial
on the subject, but I can give you the punchline:
#find_or_create_by_name(“blah”), or, alternatetively,
thanks for the link. I’ll have a look at it later. Well I know of
Mytable.delete_all but I have like 40 tables already and find it a bit
annoying to write them down by hand. I’m looking for some function that
iterates through all my tables and truncates them.
Blabla.allTablesOrModels.each do |t|
If you want to delete all of the data in all of the tables, and you
are using sqlite3, you could just delete db/development.sqlite3 and
then either rerun your migrations, or execute “rake db:schema:load”.
If you are using MySQL, then you could probably use PHPMyAdmin or the
command line to delete the data. (You could use the command line to
delete the data in an Sqlite3 database as well.)
ActiveRecord::Base.connection.tables.each do |table|
Hope this helps.
On Tue, Sep 22, 2009 at 8:34 PM, Heinz S. <
Yes, but I want it to happen automatically so I think Abhinavs code
snippet is exactly what I was looking for. Thanks!