title: ruby on rails many to many validations
body: <p>I have:</p>
  
  <pre><code>scaffold attendance event:references user:references attendance_rate:references
  scaffold user name email
  scaffold event start_time end_time
  scaffold attendance_rate rate:integer #)[1 or 0], i do not want to use boolean)
  </code></pre>
  
  <p>Associations:</p>
  
  <pre><code>event has_many :attendances
  attendance belongs_to :event
  
  user has_many :attendances
  attendance belongs_to :user
  </code></pre>
  
  <p>Problem: How can I permit one same user to "attend" the same event only once?</p>
  
  <p>Attendance.rb:</p>
  
  <pre><code>class Attendance &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :attendance_rate
    belongs_to :event
    validates :user_id, :attendance_rate_id, presence: true
    validates_uniqueness_of :event_id, :scope =&gt; [:user_id]
  end
  </code></pre>
  
  <p>Note: attendances are created in nested forms for events.</p>