title: How to validate presence of associated records in case of has_many, through: association?
body: <p>There are models with has <code>has_many through</code> association:</p>
  
  <pre><code>class Event &lt; ActiveRecord::Base
    has_many :event_categories
    has_many :categories, through: :event_categories
  
    validates :categories, presence: true
  end
  
  class EventCategory &lt; ActiveRecord::Base
    belongs_to :event
    belongs_to :category
  
    validates_presence_of :event, :category
  end
  
  class Category &lt; ActiveRecord::Base
     has_many :event_categories
     has_many :events, through: :event_categories
  end
  </code></pre>
  
  <p><strong>The issue is with assigning <code>event.categories = []</code> - it immediately deletes rows from <code>event_categories</code>.</strong> Thus, previous associations are irreversibly destroyed and an event becomes invalid.</p>
  
  <p>How to validate a presence of records in case of <code>has_many, through:</code>?</p>
  
  <p>UPD: please carefully read sentence marked in <strong>bold</strong> before answering.
  Rails 4.2.1</p>