I have been trying to create a filter for the users who applied for X job offer, but to have a better overview I will explain my association and my problem:
class Job < ApplicationRecord
belongs_to :enterprise
has_many :job_candidates
end
class JobCandidate < ApplicationRecord
belongs_to :job
belongs_to :user
end
class User < ApplicationRecord
has_many :job_candidates
end
class User < ApplicationRecord
has_many :jobs
end
I have the users (User)
who apply for the jobs (Job)
that the companies (Enterprise)
publish, for this at the moment in which the user X applies to X job, within the table (JobCandidate)
it is stored, who applied for the offer (user_id)
, since the job was applied for (job_id)
, only those 2 fields, now, therefore JobCandidate
it is related to the User and Job models, now within the Enterprise dashboard I need to print all those records JobCandidate
where their relationship job_candidate.user.enterprise.id
coincides with the current user, however the first idea that came to me was to perform a scope, but apparently within the scope of the model I can't compare it to a current session ie(current_enterprise)
, therefore the controller comes to mind, but I can't filter that data, something that would be like:
def dashboard
@job_candidates = JobCandidate.all.where(job_candidate.job.enterprise_id: current_enterprise.id)
end
It is an idea that is quite vague, because I have not been able to come up with an exact way to compare the id of the companies of the jobs to which they applied (job.enterprise.id)
with the session of the current company (current_enterprise)
, the idea is to filter both the jobs and the users to who the company will see who applied for their jobs
I do not know if I have managed to make myself understood, but I will be very grateful if you can give me a better idea, greetings!
To obtain the users (
User
) who applied for a specific job (Job
), it would be better to consult the model directlyUser
or, through a relationhas_many :through
, the modelJob
; so you will get all the users for a certain job in an objectActiveRecord::Relation
.The first thing you should do is set the relationship
has_many :through
on the modelJob
:With this relation you will be able to obtain the users for a job without the need to explicitly use the model
JobCandidates
; For example, you could get the users who have applied for the job withid = 1
:Or, if you prefer it in one line:
The result will be a collection (
ActiveRecord::Relation
) of the objectsUser
that are related, through the modelJobCandidates
, to the selected objectJob
(ie withid = 1
).So, assuming you want to display all the jobs at a particular company on one page, and each job shows the users who have applied, you would do the following:
Controller
Here I assume that you arrive at the page through a
index
where all the listed companies are, where you click on one, which would take you to the actionshow
of the other controller, passingid
the company as a parameter.In case your way of getting
id
from the company is different, just change it in the action of the controller you are using (in my example the action isshow
).View
I am assuming that the model
Job
has an attribute calledtitle
and the modelUser
has an attribute calledname.
The result will be a section (
div
) for each job (Job
), which contains the list of users (User
) who have applied for that job.