title: How can I add a validation error in before_save association callbacks
body: <p>I have two models: The Discount has and belongs to many Businsses.</p>
  
  <p>I want to validate that a Discount always have at least one business, together with another condition (for example <code>active?</code>). I tried the following:</p>
  
  <pre><code>class Discount &lt; ActiveRecord::Base
    has_and_belongs_to_many :businesses,
      before_remove: :validate_publish_status
  
    def validate_publish_status(*arg)
      if active? &amp;&amp; businesses.count == 0
        errors[:active] &lt;&lt; 'discount with no business'
      end
    end
  end
  </code></pre>
  
  <p>However this does not work (no validation errors raised) and I realized that this is probably because it is only a callback, not a validation. How can I code it so I can use the <code>errors</code> like I do I custom validation?</p>
  
  <p>The controller action I have (for ajax):</p>
  
  <pre><code>  def remove
      @business = Business.find(params[:business_id])
      if @business.in? @discount.businesses
        @discount.businesses.delete(@business)
      end
      render json: @business.as_json(only: [:id, :type, :name, :address],
                                     methods: [:city_name, :country_name]).
        merge(paths: paths_for(@discount, @business))
    rescue ActiveRecord::RecordInvalid # even tried the generic Exception
      respond_to do |f|
        f.json { render json: {error: $!.message}, status: 403 }
      end
    end
  </code></pre>