在我的代码中,管理员可以添加用户并为他们分配工作。如果管理员决定删除用户,我想检查用户可能被分配到的任何工作并将其分配给管理员。由于用户删除发生在我的 user_controller 并调用 Jobs,这是最好的方法吗?如果没有,您能否建议最有效的方法是,特别是如果用户被分配到 1-N 个工作?提前致谢。
users_controller.rb
def destroy
if admin?
jobs = Job.where("user_id = ?", params[:id]) #find jobs where user is that being deleted
jobs.each do |job|
job.update_attribute(:user_id, current_user.id) #for each job set the user_id to the admin_id
end
end
redirect_to users_url
end