title: Rails uniqueness validation with nested has_many
body: <p>I am building a Polls website and I want to achieve that a user (ip address) can vote once per poll. I have the following models:</p>
  
  <pre><code>class Poll &lt; ApplicationRecord
    has_many :answers, :dependent =&gt; :destroy
    accepts_nested_attributes_for :answers, allow_destroy: true
  end
  
  class Answer &lt; ApplicationRecord
    belongs_to :poll
    has_many :votes
  end
  
  class Vote &lt; ApplicationRecord
    validates_uniqueness_of :ip_address
    belongs_to :answer
  end
  </code></pre>
  
  <p>With my current solution a user can vote once on one poll and then he can't vote on another.
  What should I use to <code>validates_uniqueness_of :ip_address</code> per poll? 
  I tried to use scope by poll but it didn't work.   </p>
  
  <p>PS: I know IP address is not the best solution for validating unique votes.</p>