title: Using cancan the proper way
body: <p>I have a certain requirement where the views have different content based upon the type of user. Lets say I have the index action for the users controller. Then I can use cancan to authorize the action like this</p>
  
  <pre><code>authorize! :index, @users
  </code></pre>
  
  <p>Further for filtering the content I have another authorization like</p>
  
  <p><code>if can :view_all,User</code> </p>
  
  <p>Further another authorization like</p>
  
  <p><code>if can :view_some,User</code> will require another one.</p>
  
  <p>This will result in lots of conditions. Instead of this, I could have used just simple conditions like</p>
  
  <pre><code>If the user is with view_all access show him all 
  else if the user is with view_some access show him some
  else access denied
  </code></pre>
  
  <p>Cancan requires one extra query, isn't it? I might be using cancan the wrong way. So need some suggestions.</p>
  
  <p>Here is the rough snippet of my ability.rb file</p>
  
  <pre><code>    can :index, User do |user1|
        role.accesses.include?(Access.where(:name =&gt; "access1").first) || role.accesses.include?(Access.where(:name =&gt; "access2").first)
      end
  
      can :view_all, User do |user1|
        role.accesses.include?(Access.where(:name =&gt; "access1").first) 
      end
  
      can :view_some, User do |user1|
        role.accesses.include?(Access.where(:name =&gt; "access2").first) 
      end
  </code></pre>