Ruby on Rails When you don't know the cause of rollback when saving.

Introduction

This time, we will learn what to do when you do not know the cause of rollback when saving with rails.

Currently creating a site where users can register notes.

What I want to do this time Send registration (note) from from, save it in DB (linked to user) and display it in view

Where I struggled It rolls back when I register.

@ note.save! I got the following error, so I searched

ActiveRecord::RecordInvalid: Validation failed: User must exist

In summary, it seems that the user model had no value and was played by validation

solution

optional: Describe true

note.rb


class Note < ApplicationRecord
  belongs_to :user,optional: true
  validates :title, presence: true
  validates :explanation, presence: true
end

optional: What is true?

Optional: What is true for Rails belongs_to? Allowing the foreign key nil of belongs_to

reference

But,

The rollback has been resolved, but all the posted titles are displayed as 0.

スクリーンショット 2020-10-18 22.00.33.png

Cause This was because the title data type of the migration file was integer. Change it to text type and do rails db: migration: reset to solve it!

Recommended Posts

Ruby on Rails When you don't know the cause of rollback when saving.
[Ruby on Rails] Until the introduction of RSpec
When the Ruby on Rails terminal rolls back
Docker the development environment of Ruby on Rails project
Try using the query attribute of Ruby on Rails
[Ruby on Rails] Implementation of validation that works only when the conditions are met
(Ruby on Rails6) Display of the database that got the id of the database
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
Basic knowledge of Ruby on Rails
[Ruby on Rails] Change the save destination of gem refile * Note
[Rails] Addition of Ruby On Rails comment function
Let's summarize "MVC" of Ruby on Rails
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby On Rails] How to search the contents of params using include?
Implementation of Ruby on Rails login function (Session)
How to find the cause of the Ruby error
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
Recommendation of Service class in Ruby on Rails
Publish the app made with ruby on rails
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
Determine the current page with Ruby on Rails
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby on Rails] Individual display of error messages
[rails] After option useful when you want to change the order of DB columns
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
Library not loaded when trying to upgrade the version of ruby and rails s
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] How to change the column name
[Rails] I don't know how to use the model ...
[Ruby on Rails] Implementation of tagging function/tag filtering function
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Ruby on Rails Tutorial Troublesome notes when running on Windows
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
(Ruby on Rails6) Reflecting the posted content from the form
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby On Rails] How to search and save the data of the parent table from the child table
<For super beginners> Why don't you make a chatbot using "Talk API"? ?? [Ruby on Rails]
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
[Swift] If you don't know the life cycle, you will stumble on updating the drawing: For fledgling
[Ruby on Rails] Only the user who posted can edit
[Ruby on Rails] When parameter id acquisition does not work
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
[Ruby on Rails] Automatically enter the address from the zip code