Skip to main content

Rails associations

Rails • Asked by Lee Terng Gio

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.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.