Reputation: 1799
i am new to rails so any help would be much appreciated. I am currently trying to display only the user that has been invited by a recruiter to apply for a job post. Your advise would be much appreciated - thank you
i have 4 models: user / recruiter / invite / form / advert
users[jobseeker]: a user uses a form (application form) to apply for an advert (job post)
recruiters: recruiters create adverts(jobs) and can send an invite to a user[jobseeker] to apply to an advert [job post]
user has_many invites
user has_many forms
recruiter has_many invites
recruiter has_many adverts
invite belongs_to a user
invite belongs_to a recruiter
invite belongs_to an advert
form belongs_to a user
form belongs_to an advert
advert belongs_to a recruiter
advert has_many forms
class RecruitersController < ApplicationController
def dashboard
@invites = @recruiter.invites
end
end
recruiters / dashboard.html.erb
<table>
<thead>
<tr>
<th>date</th>
<th>invite info</th>
<th>jobseeker has accepted invite & has applied - view application</th>
</tr>
</thead>
<tbody>
<% @invites.each do |invite| %>
<tr>
<td><%= invite.created_at.strftime("%B %d, %Y") %></td>
<td>
<div>you invited
<b><%= link_to "#{invite.user.firstname} #{invite.user.lastname}", user_path(invite.user) %></b>
to apply for
<b><%= link_to "#{invite.advert.title}", recruiter_advert_path(invite.recruiter, invite.advert) %></b></div>
</td>
<td>
<ul>
<% invite.advert.forms.each do |form| %>
<li><%= link_to form.user.firstname, form %> form |
applied <%= form.created_at.strftime("%B %d, %Y") %></li>
<% end %>
</ul>
</td>
</tr>
<% end %>
</tbody>
</table>
i am trying to display only the user that has been invited by the recruiter to apply for the job but instead i get all the users that have applied for the job. i tried using a ".where()" method but no luck. Your advise would be much appreciated - thank you
Upvotes: 0
Views: 33
Reputation: 81
I think you'd better use a has-many-through relationship here. See the document.
class User < ActiveRecord::Base
has_many :recruiters, through: :invitations
end
class Recruiter < ActiveRecord::Base
has_many :users, through: :invitations
end
class Invitation < ActiveRecord::Base
belongs_to :user
belongs_to :recruiter
belongs_to :advert
end
By using this relationship, if you want to find all users invited by a recruiter whose id is some_number
, you can simply do the following:
@recruiter = Recruiter.find(some_number)
@invited_users = @recuiter.users
If you want to find all users who are invited by that recruiter and are related to a single job, say @advert, you can try:
@invited_users_for_some_job = @recruiter.users.
where(["invitations.advert_id = ?", @advert.id])
Upvotes: 0