I've Ctrl+C my Rails server and rebooted the machine, but Puma keeps running an old version of the code.
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" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/users/registrations_controller.rb:17
(0.3ms) ROLLBACK
↳ app/controllers/users/registrations_controller.rb:17
(0.2ms) BEGIN
On Line 17 Puma queries the Role Model. There is no reference to the Role Model in my code. Here is the users/registrations_controller.rb, which inherits devise:
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
In a past version of the user/registrations_controller I did reference Role in order to select the Administrator role, but that was unecessary. I've been trying to get the users/registrations_controller to create a Team and the current_user as a TeamMember, but then I realized my code didn't ever change with my corrections.
You want to pass a block to super so that the team is only created if the user is actually persisted:
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
And building the record off the association will make sure that Rails actually saves both records in the same transaction. Passing ids explicitly is a code smell and in this case actually a bug as @team.id
is nil as the record was not saved.