title: Using CanCanCan - I am clearly doing something obviously wrong
body: <p>Good evening, </p>
  
  <p>CanCanCan is great; however, I think I am missing something incredibly obvious, as my Model logic is pretty straight forward. </p>
  
  <ul>
  <li>A signed-in user <em>who produced the thing</em> should be able to Read, Update &amp; Delete </li>
  <li>If a signed-in user <em>not the thing author</em> should be able to Read &amp; Comment on the thing.</li>
  <li>Someone not signed in should be able to read and/or search.</li>
  </ul>
  
  <p>Steps 1 and 3 work. However, step 2 is not working - if another user is signed-in, tries to look at a thing; they see "CanCan::AccessDenied" unless it is their thing. </p>
  
  <p>Ability.rb</p>
  
  <pre><code>    if user.present?
        can :manage, List, user_id: user.id 
      else
        can [:read, :search], List 
      end
  </code></pre>
  
  <p>Is <code>user.present?</code> linked/locked to current_user in the above snippet? I've tried omitting .present? However, that still renders CanCan::AccessDenied. </p>
  
  <p>Upon this unexpected error, I thought something like the below would work, alas not. </p>
  
  <pre><code>    if user.present? 
        can :manage, List, user_id: user.id 
      elsif @list.find(params[:id]).user != user
        can [:read], List
      else
        can [:read, :search], List 
      end
  </code></pre>
  
  <p>For posterity please find my solution building on top of what Roman Alekseiev provided. </p>
  
  <p>application controller</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
      load_and_authorize_resource :parent
      load_and_authorize_resource :child, through: :parent
  </code></pre>
  
  <p>ability.rb</p>
  
  <pre><code>class Ability
    include CanCan::Ability
  
    def initialize(user)
      user ||= ::User.new
  
      if user.new_record?
        can [:read, :search], Parent
        can [:read], Child
      else
        can [:read, :search], ::Parent
        can [:read, :create, :delete], Child
        can :manage, Parent, user_id: user.id
      end
  end
  </code></pre>
  
  <p>How to use the ability in a view, for example show. </p>
  
  <pre><code>&lt;% if can? :create, @parent =&gt; Child %&gt;
  present the child markup/content
  &lt;% end %&gt;
  </code></pre>
  
  <p><a href="https://github.com/CanCanCommunity/cancancan/wiki/Nested-Resources#has_many-through-associations" rel="nofollow noreferrer">More Reading via the official documentation</a></p>