[RUBY] [For beginners] Personally thinking about the cause of the error-the idea of ​​the solution

Overview

I have summarized the ideas from identifying the cause of the error to solving it.

The other day, an engineer had the opportunity to see a portfolio error. I will write based on what I learned at that time and my usual experience.

The purpose of this article is to:

--For your own output ――Because I thought it would be helpful for people in the same situation

As a premise I think there are many ways to identify the cause. Also, if you follow this article, it is not always possible to identify the cause. Please note that.

It is written for beginners in programming. (And I am also a beginner) There may be some points that have yet to be reached. You should do this! If you have any questions, I would appreciate it if you could comment!

environment

How to identify the cause

[Premise] The method of identifying the cause here refers to a method based on what the author has learned and experience.

The method for identifying the cause is as follows. Repeat until you can identify it.

  1. Display an error statement and predict what is wrong.
  2. Find out what's wrong with the code in question.

Then, I will describe each in detail.

1. Display an error statement and predict what is wrong.

You can check the error statement by the following method.

--The red one displayed in the browser (Wow and Kanjiru) --Get out on the console

To display the error statement on the console, use the following method.

--.valid? (Method to check if saved correctly. True or false is returned.) --.errors (display errors) --.full_messages (display error statements)

The following example is used to describe how to use the above method.

** Example) Build an intermediate table with hoge model and fuga model and check if it can be saved. ** **

pry(main)> hoge = Hoge.create(name:"Hoge", fuga_ids:[1,2]) #Substitute the created one for the variable hoge.

pry(main)> hoge.valid?                                    #hoge checks if it can be saved without getting caught in validation.
=> false                                                  #True or False is returned as the return value.

pry(main)> hoge.errors.full_messages                      #.errors.full_When used with messages, an error statement is displayed.
=> fugas is invalid

As a flow, check if it can be saved with valid? ↓ If false, use errors.full_messages to display error statements. I feel said.

This time, I was able to confirm the error statement fuga is invalid.

2. Find out what is the cause of the corresponding code.

Here, the cause is identified in the following order.

  1. Predict what the cause is from the error statement.
  2. Test the hypothesis.

I will describe each of them.

** Predict what the cause is from the error statement. ** **

First of all, from the error statement, I predict that the cause of the error is probably around here. As an image, it feels like roughly deciding the direction to go. (It doesn't matter if you go west or north.)

Personally, I think about which of the following.

--Controller

Isn't the model suspicious when predicted using the example of the intermediate table above? It will be. The process I thought was suspicious was as follows.

  1. Try google "is invalid".
  2. Hmmm, there will be many articles about validation, right?
  3. Found "is invalid" in the validation error statement in the rails guide
  4. Is validation a ** model **?

I feel said.

** Hypothesis testing **

I will find out what is wrong with the part I predicted earlier. I personally think of hypothesis testing as follows.

――I'll do it anyway until I find out the cause ――Do not think too much about this, but think honestly from the error statement. ――If you get stuck on the way, use A4 paper to organize your thoughts.

If you want to test the hypothesis using the example of the intermediate table above,

――In the first place, can you create a record for each table? (Try making it using the console) ――If the validation is bad, what happens if you try to erase it? (Erase the description of validation)

It's like that.

By the way, how to organize your thoughts using A4 paper Please see here.

Idea of ​​solution

Here, we will describe the concept of the solution.

I learned to think of a solution using the following ideas.

  1. Remove the cause
  2. Bypass the cause

** Remove the cause ** It's like changing the cause and removing it. Basically, we will come up with a solution based on this.

** Bypass the cause ** It's like bypassing the cause and not causing an error. Occasionally, you will get an unexplained error. When there is nothing you can do about it, you can bypass the cause.

It ’s a bit extreme, Using the example of the intermediate table above,

Remove the cause → Example) Change to another method.

Bypass the cause → Example) Use validates: false to skip.

I feel said.

at the end

Thank you for reading until the end!

Among those who read it

――It worked! ――I'm doing this!

I would appreciate it if you could comment!

Recommended Posts

[For beginners] Personally thinking about the cause of the error-the idea of ​​the solution
About the idea of anonymous classes in Java
[For beginners] DI ~ The basics of DI and DI in Spring ~
The idea of jQuery
[For beginners] About the JavaScript syntax explained by Java Gold
The idea of C # (lambda expression, for statement) to chew
About the handling of Null
About the description of Docker-compose.yml
I will explain the nesting of for statements that kill beginners
[Android] Dynamically set the height of ListView with copy (for beginners)
About the behavior of ruby Hash # ==
About the basics of Android development
About the role of the initialize method
Think about the 7 rules of Optional
[For Java beginners] About exception handling
Summary about the introduction of Device
About the log level of java.util.logging.Logger
[Rails] Articles for beginners to organize and understand the flow of form_with