无法使用自己创建新记录加入关系 Rails 中的用户 Table 和设计

Cannot create new record with self Joins relationship User Table in Rails and Devise

我的 rails 应用程序出现问题。这是我模型中的代码:

    class User < ApplicationRecord
  has_many   :assistants,  class_name: User, foreign_key: :manager_id
  belongs_to :manager, class_name: User

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  #validation       
  validates :first_name, :middle_name, :last_name, :birth_date, presence: true


end

这是我的迁移:

    class AddReferenceToUserTable < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :manager_id, :integer
  end
end

但这是我的视图中的错误:

报名 1 个错误禁止保存此用户: 管理员必须存在

请问我的代码有什么问题吗?

谢谢, 兰兹

如果这是一个rails 5 应用程序属于是默认必需的。如果用户不会总是有经理那么你想要

belongs_to :manager, class_name: User, required: false