title: Rails ActiveRecord - Excluding Results if Record in a Different Table
body: <p>I'm trying to create a site similar to taskrabbit or upwork, where a member can post a job and a worker can submit a bid on the job. This is a follow up to a question I recently posted. I am working on the search/job display functionality, and want to return a resultset with only jobs that meet the following criteria: </p>
  
  <p>1) job start date is in the future
  2) worker hasn't already bid on the job
  3) the client hasn't already accepted a bid from a different worker
  4) the worker has the appropriate skills required for the job</p>
  
  <p>Thanks to @jvillian the first 2 requirements are complete via a class in the job model, but when I've tried several variants to exclude records where there is an entry for the job in the winning_bids table (requirement 3  - meaning the client has accepted a bid and isn’t accepting any more bids for the job) or when the worker doesn’t have all the Skills listed in the job_skills table in the worker_skills table (requirement 4 - meaning they don’t have all the Skills required for the job)</p>
  
  <p>How might I modify the search criteria in the Job class to meet requirements 3 and 4? </p>
  
  <p>Models are as follows:</p>
  
  <pre><code>class Worker &lt; ApplicationRecord
      has_many :bids
      has_many :winning_bids
      has_many :abilities, through: :job_options
  end
  
  class Skill &lt; ApplicationRecord
      has_many :job_skills
      has_many :jobs, through: :job_skills
      has_many :worker_skills
      has_many :skills, through: :worker_skills
  end
  
  class WorkerSkill &lt; ApplicationRecord
      belongs_to :worker
      belongs_to :skill
  end
  
  class Job &lt; ApplicationRecord
      has_many :bids
      has_many :skills
      has_many :job_skills
      has_many :skills, through: :job_skills
  
      def self.available_for_bid(worker)
          where.not(id: worker.job_ids).where('start_date &gt; ?', Date.today)
      end
  end
  
  class JobSkill &lt; ApplicationRecord
      belongs_to :job
      belongs_to :skill
  end
  
  class Bid &lt; ApplicationRecord
      belongs_to :job
      belongs_to :worker
  end
  
  class WinningBid &lt; ApplicationRecord
    belongs_to :job
    belongs_to :worker
  end
  </code></pre>