[Rails] What to do if data is not registered in DB

Introduction

I want to save the form input contents in the DB, but I was addicted to it, so output

environment

Rails 5.0.7.2 ruby 2.5.1 mysql 14.14

problem

When I try to save the form input, it is saved in the parameter but not in the DB.

Workaround

Check the cause by adding! After the method to save.

Then, ** validation failed: User must exist ** (error content may be different)

Cause

The cause is that the foreign key is not included when the association is formed and it is played by validation.

Describe ** optional: true **.

goal.rb


class Goal < ApplicationRecord

  validates :name, presence: true, uniqueness: true
  validates :time, presence: true, uniqueness: true
  validates :days, presence: true, uniqueness: true
  belongs_to :user, optional: true #Edit here
end

** optional: true ** allows nil for the foreign key of belongs_to.

I think you can save it in the DB with this. please refer!

Recommended Posts

[Rails] What to do if data is not registered in DB
Data is not registered in Rails.
What to do if Cloud9 is full in the Rails tutorial
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do when rails db: seed does not reflect in the database
What to do if audio is not available on discordrb
What to do if FacesMessage is set but not displayed
# What to do if you accidentally do rails db: migrate: drop
[Ubuntu 20.04] What to do if the external monitor is not recognized
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if you get an error during rails db: reset
What to do if the background image is not applied after deployment
What to do if tomcat process remains when tomcat is stopped in eclipse
What to do if the app is not created with the latest Rails version installed when rails new
What to do if the Rails server can't start
What to do if rails server can't be stopped
What to do if the changes are not reflected in the jar manifest file
[Swift] What to do if the app icon is set but not reflected
What to do if password authentication fails in Docker/Postgres
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if ffi installation fails when launching an application in Rails
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that
What to do if the update does not take effect after deploying Rails AWS
What to do when Address already in use is displayed after executing rails s
What to do if the breakpoint is shaded and does not stop during debugging
What to do if you get a java.io.IOException in GlassFish
What to do when Method not found in f: ajax
What to check when rails db: migration does not pass
What to do if you can't use the rails command
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
What to do when a could not find driver appears when connecting to a DB in a Docker environment
What to do if you get a "302" error in your controller unit test code in Rails
What to do when Rails on Docker does not reflect controller changes in the browser
What to do if you get an error in Basic authentication during Rails test code
What to do if you get a gcc error in Docker
What to do when the changes in the Servlet are not reflected
What to do if the Eclipse Maven dependency Jar is wrong
[Rails] What to do when rails s does not respond or does not stop
What to do if you get a DISPLAY error in gym.render ()
[Logback] What to do if unnecessary files do not disappear during rotation
What to do if you forget the root password in CentOS7
[Rails] What to do if you can't get parameters with form_with
[Maven] What to do if you are asked to incorporate a jar that is not in the remote repository into the war
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
What to do if you install Ubuntu
What to do if changes are not reflected after automatic deployment to EC2
What to do if you get a groovy warning in Thymeleaf Layout
What to do when Blocked Host: "host name" appears in Ruby on Rails
What to do if deployment fails on Heroku (Ruby app not detected)
What to do if you get Application with name appName is already registered. When you try to start GlassFish
What to do if the image posted by refile disappears after setting a 404 error page in Rails
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
[Rails / Docker] What to do if access is denied by the browser (localhost: 3000) after adding a gem
What to do if the server tomcat dies
What to do if you push incorrect information
What to do if mvn archetype: generate fails
After all, what is [rails db: migrate] doing?
What to do when debugging "Source not found"
[Rails] Added in devise: username not added to database
What to do when IllegalStateException occurs in PlayFramework