[Ruby] I changed the column name, but I was told the Undefined method for the column before the change

less than 1 minute read

version

・Ruby 2.5.7 ・Rails 5.2.4.3

Attempting to save a new record by changing the column name causes an error

It is called Undefined method script_id. Screenshots 2020-08-13 17 11 14

However, when I look at schema.rb (before change: script_id → after change: joke_book_id), it has changed…

schema.rb


t.integer "joke_book_id"
t.integer "user_id"
t.text "introduction"

So where is this script_id?

Conclusion: I didn’t change the model

After various investigations, I find that the model is suspicious. Looking at the model, Validated: :script_id, {presence: true}. Change here to a new column. validates :joke_book_id, {presence: true}

users_joke.rb


# validates :script_id, {presence: true} #Before change

validates :joke_book_id, {presence: true} # after change

It worked! ! !