[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
What to do if FacesMessage is set but not displayed
[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 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
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 if you get a "302" error in your controller unit test code in Rails
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
[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
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
[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
What to do if you get Could not locate Gemfile or .bundle / directory
What to do if build from command line fails in Android development environment
Androd: What to do about "The Realm is already in a write transaction in"
What to do when is invalid because it does not start with a'-'
[Rails] If CSS is not applied in collection_select, check if class can be specified.
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you get Could not save master table to file after importing a project in Eclipse
What to do if Command line is too long appears when building a gradle project in Intellij IDEA
What to do about "A server is already running ..." that happened without turning off the rails server in the terminal
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do if the debug gem installation fails
[Java] What to do if you get an error in Eclipse saying "Not allowed at source level below 1.X"
What to do if you get a wrong number of arguments error in binding.pry