1. Override the serialization_scope Method

The key issue is likely related to how Rails automatically sets the serialization_scope. By default, Rails will call current_user to establish the context for serialization. We need to override this behavior.

class YourController < ApplicationController
    def serialization_scope
      nil # Prevents Rails from using current_user as the scope for serialization
    end
  end

This ensures that current_user is not invoked during the rendering process. Setting serialization_scope to nil disables the automatic attempt to assign a scope for JSON serialization.

2. Skip Unnecessary Callbacks

Even if you’ve removed some callbacks, it’s good to ensure that any default Rails callbacks (like set_turbolinks_location_header_from_session) don’t interfere with the action.

 class YourController < ApplicationController
     skip_before_action :set_turbolinks_location_header_from_session, :clean_temp_files
  
     def test
       _process_action_callbacks.map { |c| pp c.filter }
       render json: { hello: 'world' }
     end
   end

Skipping callbacks makes sure that Rails isn’t triggering any logic (like session handling or file cleanup) that could indirectly cause current_user to be called.

Support On Demand!

Ruby on Rails