title: Ruby on Rails 3: after destroy method to destroy object if there is no more related objects in the system?
body: <p>I have a little problem, i have the following 2 models:</p>
  
  <pre><code>class CriticalProcess &lt; ActiveRecord::Base
  
  
  
    has_many :authorizations, :dependent =&gt; :destroy
    has_many :roles, :through =&gt; :authorizations
    after_destroy :check_roles
  
    def check roles
       cp_roles = self.roles
       cp_roles.each do |role|
         if role.critical_processes.size == 0
            role.destroy
         end
       end
    end
  end
  </code></pre>
  
  <p>and </p>
  
  <pre><code>class Role &lt; ActiveRecord::Base
  
    has_many :authorizations
    has_many :critical_processes, :through =&gt; :authorizations
  
  end
  </code></pre>
  
  <p>So 1 role can belong to many critical processes, is there any way I can make it that if <strong>ALL</strong> the critical processes that the role belonged to were to be destroyed, then for it to be destroyed as well? I need this because if all CP's (critical Processes) that the roles had a relationship with were to be destroyed then the role should also be destroyed as its no longer needed.</p>
  
  <p>UPDATE</p>
  
  <p>I have now created a after_destroy method which should delete the roles but this doesn't seem to be working, for some reason after debugging using the logs its not looping through the array for some reason?</p>
  
  <p>why is this?</p>
  
  <p>Thanks </p>