title: CanCan abilities via Database and load_resource
body: <p>My permissions are defined in a database like described on <a href="https://github.com/ryanb/cancan/wiki/Abilities-in-Database" rel="nofollow">CanCan wiki</a>. I want to authorize the <code>:index</code> only for the id in my permissions table, but still display the index page if no resource has been authorized.</p>
  
  <p>If I use <code>load_and_authorize_resource</code>, it loads the resources based on the permissions. If I don't have any, the <code>index</code> action isn't accessible. I get an access denied on the resource.</p>
  
  <p>If I still want to allow access to the <code>:index</code> action with no resource authorized, I see two options:</p>
  
  <p><strong>Option 1 (hate it)</strong>
  : Load manually the resources for the index.</p>
  
  <pre><code>class FirmsController &lt; ApplicationController
    load_and_authorize_resource :except =&gt; :index
  
    def index
      @firms = user.permissions.where{action: ["index", "manage", "read"], subject_class: "Firm"}
    end
  end
  </code></pre>
  
  <p><strong>Option 2 (meh)</strong> : Add an ability for the :index with a constraint of id: [] (nothing) and overwrite it with my database permissions afterward.</p>
  
  <pre><code>class Ability
    include CanCan::Ability
  
    def initialize(user)
  
      user ||= User.new # guest user (not logged in)
  
      can :create, Firm
      can :index, Firm, id: []
  
      user.permissions.each do |permission|
        if permission.subject_id.nil?
          can permission.action.to_sym, permission.subject_class.constantize
        else
          can permission.action.to_sym, permission.subject_class.constantize, :id =&gt; permission.subject_id
        end
      end
  
    end
  end
  </code></pre>
  
  <p><strong>Option 3?</strong></p>
  
  <p>Can you think of a cleaner solution?</p>