2015-12-18 33 views
0

我新的軌道所以任何幫助,將不勝感激。我目前正在嘗試僅顯示已被招聘人員邀請申請職位的用戶。您的意見將非常感謝 - 感謝你檢索從列表中通過迭代的ID - 軌道4

我有4種型號:用戶/招聘/邀請/表格/廣告

網友[求職]:用戶使用的形式(申請表)申請一個廣告(工作後)

招聘:招聘人員創造廣告(工種),並可以發送邀請給用戶[求職]申請一個廣告[工作職位]

型號:

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> 

我試圖只顯示已邀請招聘用戶申請這份工作,而是我得到的所有已應用作業的用戶。我嘗試使用「.where()」方法,但沒有運氣。您的意見將非常感謝 - 感謝你

enter image description here

+0

你試過'@ invites.where(USER_ID:id_of_user)'時,你會檢索到的'id_of_user' FR om單獨的查詢,或者您可以加入表格來執行單個查詢。 – bjhaid

回答

0

我想你最好在這裏使用一個具有一對多通關係。請參閱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 

通過,如果你想找到一個招聘id爲some_number邀請所有用戶在使用這種關係,你可以簡單地做到以下幾點:

@recruiter = Recruiter.find(some_number) 
@invited_users = @recuiter.users 

如果你想找到所有用戶誰由招聘人員被邀請,並涉及到一個單一的工作,說@advert,你可以嘗試:

@invited_users_for_some_job = @recruiter.users. 
    where(["invitations.advert_id = ?", @advert.id])