title: I don't understand how "optional: true" work on rails?
body: <p>I can't understand how to work <code>optional: true</code>. It has solved the must exist error</p>
  <p><img src="https://i.stack.imgur.com/ODB2b.png" alt="error_massage" /></p>
  <p>I’m installing the following function by rails, and the record didn't save because of validation.
  I've heard that <code>optional: true</code> permit saving nil record, but it didn't save without <code>optional: true</code> record didn't have nil though. The error occurred when I tried to access <code>relation#create</code>.</p>
  <p>My code is here ↓</p>
  <pre class="lang-rb prettyprint-override"><code>class User &lt; ApplicationRecord
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :validatable
           
    has_many :articles
    has_many :goods
    
    has_many :active_relations, class_name: &quot;Relation&quot;, foreign_key: :following_id
    has_many :passive_rerations, class_name: &quot;Relation&quot;, foreign_key: :follower_id
    
    has_many :followings, through: :active_relations, source: :follower
    has_many :followers, through: :passive_rerations, source: :following
  end
  </code></pre>
  <pre class="lang-rb prettyprint-override"><code>class Relation &lt; ApplicationRecord
    belongs_to :followings, class_name: &quot;User&quot;, optional: true
    belongs_to :followers, class_name: &quot;User&quot;, optional: true
  end
  </code></pre>
  <pre class="lang-rb prettyprint-override"><code>class RelationsController &lt; ApplicationController
    def create
      follow = current_user.active_relations.new(follower_id: params[:user_id])
      follow.save!
      redirect_to root_path
    end
  end
  </code></pre>
  <pre class="lang-rb prettyprint-override"><code>Rails.application.routes.draw do
    devise_for :user
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    root &quot;articles#index&quot;
    resources :articles, except: [:edit, :delete] do
      resources :goods, only: [:create, :destroy]
      get &quot;gooder&quot;, on: :member
    end
    resources :users, only: :show do
      resources :relations, only: [:create, :destroy]
      get &quot;followings&quot;, on: :member
      get &quot;followers&quot;, on: :member
    end
  end
  </code></pre>