[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name

Introduction

While learning Rails Tutorial Chapter 2, "2.3.1 Exploring Microposts", I generated code with scaffold with the wrong column name.

Procedure when you make a mistake

I accidentally typed: one: Micropost as "mivroposts".

$ rails generate scaffold mivroposts content:text user_id:integer

: two: I just migrated without noticing the typo.
$ rails db:migrate

The result of making a mistake

I noticed a typo only when it was displayed in routes.rb.

routes.rb


Rails.application.routes.draw do
  resources :mivroposts #What is Maiburo Posutsu?
  resources :users
  root 'users#index'
end

What I looked up

I searched google for "rails g name mistake" and found an article that describes the procedure for correcting a situation similar to myself, so I used it as a reference. Thank you very much.

[For Rails Girls participants] Corrective procedure when the column name (attribute name) is entered incorrectly when creating scaffold

What to do

: one: I was running rails db: migrate, so I ran the following command to undo the changes I made to the database.

$ rails db:rollback

: two: Scaffold has been deleted.
$ rails destroy scaffold mivroposts

: three: I generated the code again with scaffold and executed the migration.
$ rails generate scaffold Micropost content:text user_id:integer

$ rails db:migrate

Result of coping

routes.rb


Rails.application.routes.draw do
  resources :microposts
  resources :users
  root 'users#index'
end
== 20200614080432 CreateMicroposts: migrating =================================
-- create_table(:microposts)
   -> 0.0067s
== 20200614080432 CreateMicroposts: migrated (0.0077s) ========================

References

-[For Rails Girls participants] Corrective procedure when the column name (attribute name) is entered incorrectly when creating scaffold

-About manners to be careful when publishing articles on Qiita ~ Do not copy the contents of the net or books without permission ~

Summary

When I found out that I made a typo, I was surprised and wondered what to do, but at that time I thought it was important to stay calm and avoid making mistakes.

I made a mistake from the beginning of the Rails tutorial and got a little dented, but I thought that there was a lot to learn from the mistake, and it was a good opportunity to learn that it is more important to follow it than to make a mistake. did. (Of course, it's best not to make mistakes, and I can't say such a sweet thing in actual work.)

Recommended Posts

[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
What to do when Blocked Host: "host name" appears in Ruby on Rails
How to reference a column when overriding the column name method in ActiveRecord
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do when rails db: seed does not reflect in the database
What to do when rails creates a 〇〇 2.rb file
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you get a java.io.IOException in GlassFish
What to do when you launch an application with rails
What to do if you can't use the rails command
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
What to do if you get a "302" error in your controller unit test code in Rails
What you need to do to open a file from the menu in the document-Based App macOS app
What to do when Rails on Docker does not reflect controller changes in the browser
[Rails] What to do when the view collapses when a message is displayed with the errors method
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
What to do if you get a gcc error in Docker
What to do when the changes in the Servlet are not reflected
What to do if you get a DISPLAY error in gym.render ()
What to do when you become a Problem During Content Assist
What to do when you run into a docker-compose node_modules problem
What to do if you forget the root password in CentOS7
What to do when a javax.el.PropertyNotWritableException occurs
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
[Programming beginner] What to do when rails s becomes an error in the local development environment
What to do when you want to know the source position where the method is defined in binding.pry
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if you get a groovy warning in Thymeleaf Layout
[Rails] How to log in with a name by adding a devise name column
What to do when "call'Hoge.connection' to establish a connection" appears on rails c
Notes on what to do when a WebView ClassNotFoundException occurs in JavaFX 12
When you want to add a string type column with a limited length with the `rails generate migration` command
What to do if the image posted by refile disappears after setting a 404 error page in Rails
When you get lost in the class name
How to make a follow function in Rails
What do you use when converting to String?
What to do when IllegalStateException occurs in PlayFramework
What to do when you want to delete a migration file that is "NO FILE"
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
<f: ajax> Unable to attach <f: ajax> to non-ClientBehaviorHolder What to do when you become a parent
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
Androd: What to do about "The Realm is already in a write transaction in"
How to make a unique combination of data in the rails intermediate table
What to do if ffi installation fails when launching an application in Rails
What to do if you get a javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake in the IBM JDK
What to do about "A server is already running ..." that happened without turning off the rails server in the terminal
[Rails] What to do if you get an error saying "Could not find a JavaScript runtime." When executing the rails s command on Catalina
What to do if the Rails server can't start
What to do when The SSL certificate has expired
[Ruby on Rails Tutorial] Error in the test in Chapter 3
What to do if you accidentally create a model
What to do when a null byte error occurs
[Rails] How to change the column name of the table