title: Delete Orphans in HABTM ruby on rails
body: <pre><code>class Actor &lt; ActiveRecord::Base
  
    has_and_belongs_to_many :movies
  
    before_destroy :remove_orphaned_initialize
    after_destroy :remove_orphaned
  
  
  private
  
    def remove_orphaned_initialize
      @instvariable = self.movies
    end
  
    def remove_orphaned
      @instvariable.each do |m|
        m.destroy if m.actors.empty? and m.titles.empty?  ##movies also have habtm with Title
      end
    end
  
  end
  </code></pre>
  
  <p>if i delete an actor record
  then the orphan movie is not deleted because the @instvariable is nil in remove_orphaned</p>
  
  <p>could you explain how i could achieve it.</p>
  
  <p>if i try to cal remove_orphaned before destroy , then m.actors wil never be empty..
  coz the present doctor instance is not yet deleted</p>