Ask A Question


You’re not receiving notifications from this thread.

Rails associations

Lee Terng Gio asked in Rails

I'm create a Ruby on Rails web application. I have 4 models, which are User, Job, Candidate, ApplyingJob. User is the one who will be using the web application and the user can create Job and Candidate, while the from the Candidate, the candidate will be able to add an ApplyingJob and ApplyingJob will carry the same information from the Job. Below are my associations,

has_many :candidates
has_many :jobs

belongs_to :user
has_many :applyingjobs

belongs_to :user
has_one :applyingjob

belongs_to :candidate
belongs_to :job

Here's my problem, I want to let the User be able add an ApplyingJob in the Candidate's profile, and that particular ApplyingJob belongs_to that Candidate and also the Job. ApplyingJob carries 2 foreign keys, which are candidate_id and job_id. If the User creates a new ApplyingJob from the Candidate's profile, the ApplyingJob is associated to the Candidate since there is a candidate_id, but how does it associate to the Job because the User creates a new ApplyingJob in the Candidate's profile?


Think you could get rid of the has_many :candidates and have a Candidate belong_to a Job? From a quick glance all associations should from then be correct. You could do then The join table would never have to called upon.
Also check if the has_one :applyingjob is correct.


@geometry dash
Excellent advice that is easy to understand. This will undoubtedly come in handy when the time comes for me to launch my blog.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,969+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.