Given the following classes:
class Candidate
has_many :applications
has_many :companies, :through => :job_offers
end
class JobOffer
belongs_to :company
end
class Application
belongs_to :candidate
belongs_to :job_offer
end
How can I validate the previous statement (in the image) on Rails?
Adding the following validation on Application won't work when updating:
def validate_uniqueness_of_candidate_within_company
errors.add(:job_offer_id, "...") if candidate.companies.include?(company)
end
Cause when trying to change the application to a different JobOffer of the same company candidate.companies will return that company.
I also tried doing something like this on Application:
validates_uniqueness_of :user_id, :scope => {:job_offer => :company_id}
But it didn't work either. Any ideas to solve this without having to use 10 lines of crappy code?