Puma 似乎没有为设计 user/registrations_controller.rb 执行当前代码库

Puma doesn't seem to be executing current codebase for devise user/registrations_controller.rb

我已经 Ctrl+C 我的 Rails 服务器并重新启动了机器,但是 Puma 保留了 运行 旧版本的代码。

Redirected to http://printrdwn.com:3000/
   (0.3ms)  BEGIN
  ↳ app/controllers/users/registrations_controller.rb:17
  Role Load (1.2ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."id" =  LIMIT   [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/users/registrations_controller.rb:17
   (0.3ms)  ROLLBACK
  ↳ app/controllers/users/registrations_controller.rb:17
   (0.2ms)  BEGIN

第 17 行 Puma 查询角色模型。我的代码中没有对角色模型的引用。这是users/registrations_controller.rb,它继承了设计:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    super

    @team = Team.new(name: "Personal")
    TeamMember.create(user_id: current_user, team_id: @team.id, role_id: 1)
    @team.save
  end
end

在 user/registrations_controller 的过去版本中,我确实参考了 Role 以便 select 管理员角色,但这是不必要的。我一直在努力让 users/registrations_controller 创建团队,让 current_user 作为团队成员,但后来我意识到我的代码并没有随着我的更正而改变。

您想将块传递给 super,以便仅在用户实际保留时才创建团队:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    super do |user|
      @team = Team.new(name: "Personal")
      @team.members.new(user: user, role_id: 1)
      @team.save
     end
  end
end

并且建立关联记录将确保 Rails 实际上将两条记录保存在同一个事务中。显式传递 ID 是一种代码味道,在这种情况下实际上是一个错误,因为 @team.id 是 nil,因为记录未保存。