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

Re: How to delete a model?

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.

Railscasts - Free Ruby on Rails Screencasts

Re: How to delete a model?

thanks...it would be nice if someone can write some command to delete unwanted model

Re: How to delete a 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.

God is a storyteller.

Re: How to delete a model?

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.

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: How to delete a model?

For reference: script/destroy controller controllername will work similarly, without any nasty database issues at all.

God is a storyteller.

Re: How to delete a model?

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

Re: How to delete a model?

Cool, I never knew about script/destroy. I need to get out more.

Railscasts - Free Ruby on Rails Screencasts

Re: How to delete a model?

What about if you have already run the migration, how do you delete the model from the database???

Re: How to delete a model?

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)

On n’est bien servi que par soi-même

Re: How to delete a model?

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.

Re: How to delete a model?

rails destroy model user

This should work

Re: How to delete a model?

rails destroy model user
worked great for my needs.  Thanks smile

Re: How to delete a model?

rails destroy model user,