Topic: How to delete a model?
Hi, i accidentally created a model that i don't want to use:
ruby script/generate model user
is there any command for deleting a model completely?
Thanks
You are not logged in. Please login or register.
Rails Forum - Ruby on Rails Help and Discussion Forum » Other Rails Programming » How to delete a model?
Hi, i accidentally created a model that i don't want to use:
ruby script/generate model user
is there any command for deleting a model completely?
Thanks
Not that I know of. You'll have to remove the files manually. When you run the command it lists the files it generated. You can use this as a reference to see which files should be removed.
thanks...it would be nice if someone can write some command to delete unwanted model
If you haven't already updated the database, then running the command
>ruby script/destroy model user
should be fine. The 'destroy' script does exactly what you're asking for - deletes the files that 'generate' created.
Beware, though - when you generate a model, it creates a database migration. If you run 'destroy' on that model, it will delete the migration file, but not the database table. Since generating a model really only creates two files (user.rb in app/models, and ###_create_users.rb in db/migrate) it might be simpler to manually delete the model file, and create a new migration file to destroy the users table.
Hope that helps - I may not know much about Ruby, but I know a LOT about having to correct my own mistakes.
There is script/destroy model modelname..... However I don't tend to recommend it, unless its being used immediately after script/generate. Since it will delete everything created by the script/generate command which include the migration file.... If you've already created new migrations after making the model, this makes things unhappy.
For reference: script/destroy controller controllername will work similarly, without any nasty database issues at all.
Thanks guys...the only reason why i wanted a command like 'destroy' is because sometimes i have typo, u know like, 'script/generate model uesr' instead of 'user'.It's frustrating to go through folders just to delete them, this command will come in really handy. Thanks again for the tip
Cool, I never knew about script/destroy. I need to get out more.
What about if you have already run the migration, how do you delete the model from the database???
If you already ran 'rake db:migrate', in addition to steps given above, just delete the model's table from the database (for instance for model 'User' it would be a table 'users'). When you are running 'rake db:migrate' it also updates the value in 'version' column of the 'schema_migrations' table, which is a datetime of the last model migration ran. But I think this can be safely left as is, as next migration you create will have future datetime stamp related to this field.
Last edited by oberon (2010-08-18 16:31:11)
I just got started but here's what I do when I create a model, controller, or scaffold that I either don't want or want to recreate.
First I undo the changes to the table.
>rake db:rollback
Next I delete the model, controller, or scaffold. The destroy command deletes everything that the generate command creates.
>rails destroy model Modelname
I think this is what you were looking for.
rails destroy model user
This should work
rails destroy model user
worked great for my needs. Thanks ![]()
rails destroy model user,
Hosting provided by aTech Media