Validate uniqueness of in association

前端 未结 4 1102
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 12:46

Given the following classes:

class Candidate
  has_many :applications
  has_many :companies, :through => :job_offers
end

class JobOffer
  belongs_to :company         


        
4条回答
  •  暖寄归人
    2021-02-05 13:45

    this is how I will structure the models:

    class Candidate
      has_many :applicatons
      has_many :job_offers
      has_many :offering_companies, :through => :job_offers
    end
    
    class Application
      belongs_to :candidate
    end
    
    class JobOffer
      belongs_to :candidate
      belongs_to :company
    
      validates_uniqueness_of :candidate_id, :scope => :company_id
    end
    
    class Company
    end
    

    This should work! Btw. I will call the application something else to avoid the naming confusion( rails already has application.rb

提交回复
热议问题