Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:07:15+00:00 2026-05-28T19:07:15+00:00

Given the following classes: class Candidate has_many :applications has_many :companies, :through => :job_offers end

  • 0

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

enter image description here

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:07:16+00:00Added an answer on May 28, 2026 at 7:07 pm

    There are likely many acceptable approaches to solve your issue but I think the bottom line is that you’re trying to enforce a uniqueness constraint on the table that doesn’t (directly) have all the attributes (both company AND user). I’d de-normalize the company info into the application table (user_id, job_offer_id, company_id) and always set it in a before_save callback to match the job_offer‘s company. Then you should be able to use the scoped uniqueness validation:

    class JobApplication < ActiveRecord::Base
      belongs_to :user
      belongs_to :job_offer
      belongs_to :hiring_company, :class_name=>"Company", :foreign_key=>"company_id"
    
      before_save :set_hiring_company
    
      def set_hiring_company
       self.hiring_company = job_offer.hiring_company
      end
    
      validates_uniqueness_of :user_id, :scope => :company_id
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following two classes: class Location < ActiveRecord::Base belongs_to :holiday_schedule validates :name, :presence
Given the following classes and interfaces class A{ @NotNull(groups=Section1.class) private String myString } interface
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
Given the following classes: @XmlRootElement(name = parent) class Parent { private Child child; //
Given the following classes: <?php class test{ static public function statfunc() { echo this
Given the following classes: class Foo { struct BarBC { protected: BarBC(uint32_t aKey) :
Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) {
Given the following POCO classes: public class Certification { public int Id { get;
Given the following assemblage of classes (contrived): public class School { [PrimaryKey] public string
Given the following classes: class foo { private: int c; public: foo( int a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.