title: Getting uniqueness on multiple model association
body: <p>There are three models:</p>
  
  <pre><code># Table name: activities_people
  #
  #  activity_id :integer          not null
  #  person_id   :integer          not null
  #  date        :date             not null
  #  id          :integer          not null, primary key
  #
  # Table name: activities
  #
  #  id          :integer          not null, primary key
  #  name        :string(20)       not null
  #  description :text
  #  active      :boolean          not null
  #  day_of_week :string(20)       not null
  #  start_on    :time             not null
  #  end_on      :time             not null
  </code></pre>
  
  <p>Third model is people.
  Associations:</p>
  
  <pre><code>class Activity &lt; ActiveRecord::Base
    has_many :activities_people
    has_many :people, through: :activities_people
  end
  
  class ActivitiesPerson &lt; ActiveRecord::Base
    belongs_to :person
    belongs_to :activity
  end
  
  class Person &lt; ActiveRecord::Base
  has_many :activities, through: :activities_people
  end
  </code></pre>
  
  <p>Issue:
  I don't know how to create validation method in ativities_person.rb to protect joining on activities, that overlaps timely.</p>
  
  <p>Example:</p>
  
  <p>person_id:1 join to activity, that will 08.08.2016(date) and last 09:00(start_on) - 10:00(end_on).
   Same person want save to another activity, that will also be 08.08.2016 and last 09:30(start_on) - 10:30(end_on). Now 
  validation should throw error that he's save to other activity in same time(time ranges overlap). Validation first should check that dates match, next should check time overlaps of activities. </p>
  
  <p>What I tried:</p>
  
  <pre><code>  def check_join_client
      # check if client joined to some activities in same date as current join
      activities_date = person.activities_people.where(date: date)
  
      if activities_date.any?
        # get start_on and end_on for current activity and date
        get_activities = person.activities.where(id: activities_date.pluck(:activity_id))
        ol_activities = get_activities.where('((start_on &lt;= :start_on AND
                                                      end_on &gt;= :end_on) OR
                                                     (start_on &lt;= :start_on AND
                                                      end_on &lt;= :end_on AND
                                                      end_on &gt;= :start_on) OR
                                                     (start_on &gt;= :start_on AND
                                                      start_on &lt;= :end_on AND
                                                      end_on &gt;= :end_on))',
                                                    start_on: get_activities.pluck(:start_on),
                                                    end_on: get_activities.pluck(:end_on))
      end
    end
  </code></pre>
  
  <p>My method isn't correct. I'm confuse how to resolve my problem.</p>