Hi, this is Teppei Mimachi.
When I'm programming, I always encounter an error. Some errors are easy to resolve, while others are unmotivated and can't be resolved in hours or even days.
Here's one technique that you might be able to use for errors that can help you when you're discouraged. This time, the error occurred in Ruby on Rails, but if it is a database-related error, it is effective in any programming language development environment regardless of Rails.
This time, it took 3 days from the occurrence of the error to solve it, so I tried to summarize it in a short and easy-to-understand manner.
To outline this article __ If the database looks like the original, it's faster to recreate the database. __ The story.
Then, have a three-day fight!
undefined method `image_name' for nil:NilClass
When I picked up the above sentence and ignored the for nil: NilClass part It means __image_name method is not defined __.
__1. Forgot to define a method 2. Simple typographical errors __
I doubted this area and tried various things, Method definition Neither bad nor typographical errors ...
Even if I try to google, I can not reach the correct answer ...
For the time being, I was tired and fell asleep on the first day.
I re-examined for typographical errors, but there was no problem.
I didn't pay much attention here
for nil:NilClass
I will try to google about the part of.
By the way, if you have never used Ruby, What is nil? May be, but
__nil is synonymous with null. __ In other words, __ "nothing" __.
One thing that comes to mind is that there is nothing ... __ It seems to be related to the database __
And when I noticed, the date had changed completely at 1 am.
Ruby on Rails 5 --undefined method ʻimage_name'for nil: NilClass error | teratail
I arrived at the above site. From here, the citation continues,
![Screenshot 2020-09-09 16.20.17.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/688029/9207fcde-3ea3-61aa-649b -0533a6d40940.png)
This exchange is everything.
For those who do not want to read with many letters and small letters, to briefly summarize the conclusion, __ I've roughly grasped the cause, but it's better to recreate the database. __ The story.
__posts Create a table and enter multiple data ↓ Program in the middle to a rule that should not create columns in nil state (nothing) ↓ Create a new column (user_id) ↓ All user_id columns up to now are in the nil state ↓ Error occurred __
The cause contains data that should not be stored in the database. Then delete the data. Or if there is no problem even if you delete it in the database of the development environment, you can restore it by recreating it like this time.
In the case of Ruby on Rails,
Rails
$ rails db:drop #Delete database
$ rails db:create #Create a database
$ rails db:migrate #Migrate to database
If you enter the source code that suits your development environment in the same way as this, it's a good translation ^ ^
Recommended Posts