Home>

Api :: V1 :: ApiController is created using namespaces in rails.
I am trying to read the model with api and return the response.

Error message

As an error, if you try to operate a model in the Controller, Api :: V1 :: is automatically added to the beginning of the specified model and an uninitialize constant error occurs.
(The following is the case of Social :: User.find)

uninitialized constant Api :: V1 :: Social :: User
Applicable source code
# api/v1/api_controller.rb
class Api :: V1 :: ApiController<ActionController :: Api
Def test
User = Social :: User.find 1
Render status: 200, json: {name: user.name}
  end
end
# config/routes.rb
namespace: api, {format: 'json'} do
  namespace: v1 do
      get 'api_test', to: 'api # test'
  end
end
# social/user.rb
module Social
  class User<ApplicationRecord
  end
end
Tried

The model can be operated on the rails console without any problems.
The phenomenon that the above namespace is given only by operation from the controller has occurred.

user = Social :: User.find 1
Supplemental information (FW/tool version etc.)

rails 5.2.0
ruby 2.2.7

  • Answer # 1

    Please call it explicitly at the top level

    user = :: Social :: User.find 1