theDrifter
theDrifter

Reputation: 1706

Nested namespaces in rails error accessing model with same name as namespace

I have a manager and a customer controller. When i want to list all the customers for a specific manager, i used to do it through the manager show action (because it was specific to a manager). So if a customer_id was found a different return value would be generated. But that left we with (sometimes huge) if branches.

Class Api::V1::ManagerController < ApiBaseController
   def show 
     if params.key?[:customer_id]
         ....
     else
         ....
     end
     render ..., status: 200
   end

To improve my design i introduced additional name spaces for sub resources. So to list all the customers for one specific manager i have a customer controller under the manager name space. There all actions specific to a manager related to a customer resource are going.

 Class Api::V1::Manager::CustomerController < Api:ApiBaseController
  def show
      Manager.find(params[:id] ...
      ...
     render ..., status: 200
 end

The routes.rb entry looks now like this

get 'manager/:manager_id/customer' => 'manager/customer#show'

When testing the new setup i receive now this error

"error":"uninitialized constant Api::V1::Manager::CustomerController::Manager

When i replace the Manager.find(..) line with another resource it is working, why cann't i access the Manager Resource anymore? I think it has something to do with the name of the namespace, but even renaming the namespace did not help.

Upvotes: 0

Views: 684

Answers (1)

Dan Andreasson
Dan Andreasson

Reputation: 16232

Try

::Manager.find(params[:id])

Prefixing with :: will access the root namespace.

Upvotes: 3

Related Questions