title: rails cancan access is denied
body: <p>I have combined custom authentication with CanCan and to my frustration it is not exactly working as expected. So, here is the snippet of code in my controller</p>
  
  <pre><code>class Profile &lt; ActiveRecord::Base
      belongs_to :user, :inverse_of =&gt; :profile
      delegate   :email, to: :user
      validates_presence_of :user_id
  end
  
  class Api::V1::ProfileController &lt; Api::V1::UserActivityController
  
      load_and_authorize_resource :user
      load_and_authorize_resource :profile, :through =&gt; :user, :class =&gt; "Profile"
  
      def index
         @profile = subject_user.profile
         authorize! :index, @profile
      end
  end
  </code></pre>
  
  <p>Here is the <code>Ability</code> class</p>
  
  <pre><code>class Ability
     include CanCan::Ability
  
     def initialize(user)
  
         alias_action :index, :show, :to =&gt; :read 
  
         can :read, Profile
      end
  end
  </code></pre>
  
  <p>If I try <code>can :read, :all</code> it works fine but with <code>can :read, Profile</code> it fails. I had the initial hypothesis that maybe the delagate in the <code>Profile</code> class causes the denial but that is not the case. any thoughts?</p>
  
  <p>The page is supposed to render into JSON using rabl</p>
  
  <pre><code>object @profile
  
  attributes :first_name, :last_name, :gender
  </code></pre>
  
  <p>-- <strong>Update</strong> --</p>
  
  <p>I figured that namespacing and nesting causes the issues with my code. My controlled is in fact namespaced (I updated the controller code) and I am trying to access <code>Profile</code> throught he following routing:</p>
  
  <pre><code>namespace :user
      namespace :profile
  end 
  </code></pre>
  
  <p>I tried the following code for <code>load_and_authorize_resource</code> in <code>ProfileController</code> (also updated the controller code above) but it is not working. Any thoughts on how it should be modified? Does anything need to be changed in the <code>Ability</code> class?</p>
  
  <pre><code>load_and_authorize_resource :user
  load_and_authorize_resource :profile, :through =&gt; :user, :class =&gt; "Profile"
  </code></pre>