All threads / Rails associations

Ask A Question

Notifications

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,

User
has_many :candidates
has_many :jobs

Job
belongs_to :user
has_many :applyingjobs

Candidate
belongs_to :user
has_one :applyingjob

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 User.first.jobs.first.candidates. The join table would never have to called upon.
Also check if the has_one :applyingjob is correct.

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.