title: Rails validate model which receive values using accepts_nested_attributes_for
body: <p>I have 2 models: <code>Dealer</code> &amp; <code>Location</code>.</p>
  
  <pre><code>class Dealer &lt; AR::Base
    has_many :locations
  
    accepts_nested_attributes_for :locations
  
    validate :should_has_one_default_location
  
    private
  
    def should_has_one_default_location
      if locations.where(default: true).count != 0
        errors.add(:base, "Should has exactly one default location")
      end
    end
  end
  
  class Location &lt; AR::Base
    # boolean attribute :default
    belongs_to :dealer
  end
  </code></pre>
  
  <p>As you understood, <code>should_has_one_location</code> adds error everytime, because <code>.where(default: true)</code> makes an sql query. How can I avoid this behaviour?</p>
  
  <p>The very dirty solution is to use combination of <code>inverse_of</code> and <code>select</code> instead of where, but it seems very dirty. Any ideas?</p>