nadia
nadia

Reputation: 2539

Delete model and migration file rails 4

I was attempting to add a tagging model to my rails blog. But I accidentally generated a "Tags.rb" model as opposed to "Tag.rb", after reading the guide I realized that making "tag" plural was a mistake when it comes to model. I rolled back the migration using

  rake db:rollback 

and then

  rails destroy model Tags.rb 

and this was what i got back

  invoke  active_record
  remove    /home/migration/templates/create_table_migration.rb
  remove    app/models/tags.rb.rb
  invoke    test_unit
  remove      test/models/tags.rb_test.rb
  remove      test/fixtures/tags.rbs.yml

When I come back to the model folder though its still there. Please help :)

Upvotes: 5

Views: 18309

Answers (6)

John
John

Reputation: 10079

First off, when generating a Rails model using rails generate model, don't add .rb to the end of the model name. Rails automatically does that so if you add .rb yourself then you end up with a model named Tag.rb.rb, which is invalid.

You're running the remove generator with "Tags.rb". Did you generate the model originally using rails generate model Tags.rb? If you did, then running rails destroy model Tags.rb will hopefully remove all the files, but since the model name is invalid, it might not work as expected (your use case is outside the intended use case of the generator). If you originally generated the model using rails generate model Tags, then running rails destroy model Tags should work.

If you originally invoked rails generate model Tags.rb, and running the remove command isn't working (which again, in understandable given that Tags.rb is an invalid name--would have been nice if the generator had originally prohibited you from generating a Tags.rb.rb file, but you can't have everything):

  1. If you can afford to destroy your database and rerun all the migrations, the easy solution will be to manually remove the added files, destroy the database, and rerun all migrations. Then you can add the correct model using rails generate model Tag.
  2. If you can't afford to destroy your database, then you'll need to manually find the problem migration in the migration's folder, run the down command on that migration, and then manually delete that migration file and all the other files.

Upvotes: 0

Antony Mithun
Antony Mithun

Reputation: 161

try this

bundle exec rake db:rollback

and then delete your model as

rails destroy model <model_name>

now totaly deleted.

Upvotes: 1

Bharat soni
Bharat soni

Reputation: 2786

You can delete the migration files in migrate folder and model in your model folder and then you can again create new one which you wants.

you have to run that commands

rails g model tag

Then migrate database

rake db:migrate

Upvotes: 0

Josh
Josh

Reputation: 8596

You can manually delete the files, or just manually remove the "s"s.

Upvotes: 1

strivedi183
strivedi183

Reputation: 4831

Try using tags plural without the additional .rb, like

rails destroy model Tags

Hope this helps

Upvotes: 1

prash
prash

Reputation: 121

Just give

rails destroy model Tag

Upvotes: 4

Related Questions