Devise 中的子用户 Rails 6

Sub-Users in Devise on Rails 6

参考之前提出并回答的问题:sub-users-and-devise OP 询问如何设计子用户。给出的答案是在用户模型中建立一个 belongs_to has_many 关系,因此:

 class User
  belongs_to :parent, :class_name => 'User'
  has_many :children, :class_name => 'User'
  ...
 end

并这样修改控制器:

class UsersController < ApplicationController  
  def new
    @user = User.new
    @user.parent_id = params[:parent_id] 
    respond_to do |format|
  end
  end

对于我正在尝试做的事情来说,这似乎是一个完美的解决方案,但我很难思考要完成这项工作需要什么样的迁移。我正在使用 Devise,我的模型和用户 table 已经存在,所以我需要实际生成一个迁移。它会像在 users table 中添加一个 parent_id 列一样简单吗?使用 add_reference 迁移不是更好吗?

我试过这个:

class AddUsersToUser < ActiveRecord::Migration[6.0]
  def change
    add_reference :users, :user, foreign_key: true
  end
end

但我得到的是 user_id 字段,而不是 parent_id 字段,因此任何引用 parent_id 的代码(例如视图或助手)都会出错

undefined method `parent_id'.

有人可以帮助我了解完成这项工作需要什么样的迁移吗?

或者是否有更好的方法在 Devise 中设置子用户?

如果您需要,这里是我的应用程序中的相关文件:

路线:

Rails.application.routes.draw do

  devise_for  :users, controllers: {
              :registrations => "users/registrations"
  }
...
end

控制器:

class Users::RegistrationsController < Devise::RegistrationsController
  # before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  def new
    build_resource
    resource.parent_id = params[:parent_id]
    yield resource if block_given?
    respond_with resource
  end
end

型号:

class User < ApplicationRecord
  belongs_to :parent, :class_name => 'User'
  has_many :children, :class_name => 'User', dependent: :destroy

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

产生错误的助手:

module UsersHelper

  # Returns the Gravatar for the given user.
  def gravatar_for(user, options = { size: 50 })
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=40&d=blank"
    image_tag(gravatar_url, class: "gravatar")
  end

  def current_user_is_head_of_household
    current_user.parent_id.nil?
  end

  def current_user_is_member_of_household
    !current_user.parent_id.nil?
  end
end

调用current_user_is_head_of_household的视图:

<h1>Lobby</h1>
<p>This is slated to be the jumping off point (home) where members can access the blog, training, personal information tracker and the various calculators.</p>

<% if current_user_is_head_of_household %>
  <%= link_to "Add user to your household", new_user_registration_path %>
<% end %>

最后抛出错误:

undefined method `parent_id' for # Did you mean? parent parent=

    Extracted source (around line #11):

9
10      def current_user_is_head_of_household
11        current_user.parent_id.nil?
12      end
13    
14      def current_user_is_member_of_household

迁移以添加父引用和外键

class AddParentToUser < ActiveRecord::Migration[6.0]
  def change
    add_reference :users, :parent, index: true
    add_foreign_Key :users, :users, column: :parent_id
  end
end

用户模型关联

  belongs_to :parent, :class_name => 'User', optional: true
  has_many :children, :class_name => 'User', foreign_key: :parent_id