Filtering records of associated tables that match the current session Rails

0

I have been trying to filter 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 users (User) who apply for jobs (Job) that companies (Enterprise) publish, for this at the time when user X applies to X employment, within the table (JobCandidate) is stored , who applied to the offer (user_id) , since work is postulated (job_id) , only those 2 fields, now well, therefore JobCandidate is related to the models User and Job, now well within the Enterprise dashboard I need print all those records of JobCandidate where their relation job_candidate.user.enterprise.id matches the current user, however the first idea that came to me was to make a scope, but apparently within the scope of the model I can not compare it with a current session is say (current_enterprise) , therefore the controller comes to mind, but I can not filter that data, something that would be like:

def dashboard
  @job_candidates = JobCandidate.all.where(job_candidate.job.enterprise_id: current_enterprise.id)
end

This is an idea that is rather vague, because I can not arrive at an exact way of comparing the id of the companies of the jobs to which (job.enterprise.id) were nominated with the current company session (current_enterprise) , the idea is to filter both the jobs and the users who the company will see who applied for their jobs

I do not know if I've managed to make myself understood, but I'll be very grateful if you can give me a better idea, regards!

    
asked by Hector Hernandez 06.09.2017 в 08:53
source

1 answer

0

To obtain the users ( User ) who applied for a specific job ( Job ), it would be better to consult directly the model User or, well, through a relation has_many :through , the model Job ; this way you will get all the users for a certain job in a ActiveRecord::Relation object.

The first thing you should do is set the has_many :through in the Job model:

class Job < ApplicationRecord
  belongs_to :enterprise
  has_many   :job_candidates
  has_many   :users, through: job_candidates
end

With this relationship you will be able to obtain the users for a job without the need to explicitly use the JobCandidates model; for example, this way you could get the users who have applied for employment with id = 1 :

@job   = Job.find(1)
@users = @job.users

Or, if you prefer on one line:

@users = Job.find(1).users

The result will be a collection ( ActiveRecord::Relation ) of the User objects that are related, through the JobCandidates model, with the selected Job object (i.e. with id = 1 ).

So, assuming you want to show on a page all the jobs of a particular company and that each job shows the users who have applied, you would do the following:

Driver

def show
  @enterprise = Enterprise.find(params[:id])
end

Here I assume that you get to the page through a index where all the listed companies are, where you click on one, which would take you to the action show of the other controller, passing as parameter the id of the company.

In case your way of obtaining id of the company is different, simply change it in the action of the controller that you are using (in my example the action is show ).

Vista

<% @enterprise.jobs.each do |job| %>
  <div>
    <h3><%= job.title %><h3>
    <ul>
      <% job.users.each do |user| %>
        <li><%= user.name %></li>
      <% end %>
    </ul>
  </div>
<% end %>

I'm assuming that the model Job has an attribute called title and the model User has an attribute called name.

The result will be a section ( div ) for each job ( Job ), which contains the list of users ( User ) that has been nominated for that job.

    
answered by 07.09.2017 / 16:46
source