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!
[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.
Then, I will describe each in detail.
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.
Here, the cause is identified in the following order.
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.
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.
Here, we will describe the concept of the solution.
I learned to think of a solution using the following ideas.
** 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.
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