0

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:

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

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)

How might I modify the search criteria in the Job class to meet requirements 3 and 4?

Models are as follows:

class Worker < ApplicationRecord
    has_many :bids
    has_many :winning_bids
    has_many :abilities, through: :job_options
end

class Skill < ApplicationRecord
    has_many :job_skills
    has_many :jobs, through: :job_skills
    has_many :worker_skills
    has_many :skills, through: :worker_skills
end

class WorkerSkill < ApplicationRecord
    belongs_to :worker
    belongs_to :skill
end

class Job < 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 > ?', Date.today)
    end
end

class JobSkill < ApplicationRecord
    belongs_to :job
    belongs_to :skill
end

class Bid < ApplicationRecord
    belongs_to :job
    belongs_to :worker
end

class WinningBid < ApplicationRecord
  belongs_to :job
  belongs_to :worker
end
Mike N.
  • 89
  • 8
  • @jvillian a follow up to a question you were a great help to before, any way you might be able to point me in the right direction here? – Mike N. May 04 '18 at 18:10
  • Do you really need a seperate table for WinningBid? In what way is it substantially different from the normal Bid model? I would just add an association to the Job model `belongs_to :winnning_bid, class_name: 'Bid'` – max May 04 '18 at 18:33
  • Which database? – max May 04 '18 at 18:47
  • I think you need to use ActiveRecord::Merge for this. [This](https://gorails.com/blog/activerecord-merge) is one of the first results on Google, but basically AR::Merge will allow you to query a table based on a condition on a related table. – Daryll Santos May 04 '18 at 19:38
  • Somebody helped you with 1, 2 from 4. Then you come back for 3, 4 from 4. Do you play a game "code it for myself and take my money?"? What have you done on your own if all of your code is a product of stackoverflow? – AntonTkachov May 04 '18 at 22:41
  • @DaryllSantos thanks I came across that but couldn't get it implemented – Mike N. May 06 '18 at 00:18
  • @max thanks for the idea but that didn't seem to work – Mike N. May 06 '18 at 00:19
  • still haven't figured out #4 but was able to get #3 working with the following code, modified a bit from the following SO answer I found by searching more (and I have done a lot of digging trying to find an answer) https://stackoverflow.com/questions/10355002/rails-scope-to-check-if-association-does-not-exist?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Mike N. May 06 '18 at 00:22

0 Answers0