Step 1 建立 job-relationship 的 Model

rails g model job_relationships job_id:integer user_id:integer 

app/model/job_relationship.rb
class JobRelationship < ApplicationRecord
  belongs_to :user
  belongs_to :job 
end 

rake db:migrate 

config/routes.rb
resources :job_relationships 
app/model/job.rb
class Job < ApplicationRecord
  has_many :job_relationships, dependent: :destroy
  has_mamy :applicants, though: :job_relationships, source: :user 
end
app/model/user/rb
class User < ApplicationRecord
  has_many :job_relationships
  has_many :applied_jobs, through: :job_relationships, source::job 

  def has_applied?(job)
    applied_jobs.include?(job)
  end

  def apply!(job)
    applied_jobs << job        # "<<" 加入job array 以后再纠结

  end
end 

Step 2 建立 job-relationship 的 Controller

rails g controller job_relationship 

app/controllers/job_relationships_controller.rb
class JobRelationshipsController < ApplicationController
    def index
    @jobs = current_user.applied_jobs.recent.paginate(:page => params[:page], :per_page => 5)
  end
  
  def show
    @job = Job.find(params[:id])  
  end 
end 
touch app/views/job_relationships/index.html.erb 

Step 3 在 resumes controller中加入

Step 4 在 jobs/show.html.erb 中加入

app/views/jobs/show.html.erb
<div class="text-center">
  <% if !current_user %>
    <%= link_to("Submit Resume", new_job_resume_path(@job), class: "btn btn-primary", method: :get) %>
  <% else %>
    <% if current_user.admin? %>
      <%= link_to("Submit Resume", new_job_resume_path(@job), class: "btn btn-primary disabled", method: :get) %>
    <% else %>
      <% if current_user.has_applied?(@job) %>
        <%= link_to("Has Applied", new_job_resume_path(@job), class: "btn btn-primary disabled", method: :get) %>
      <% else %>
        <%= link_to("Submit Resume", new_job_resume_path(@job), class: "btn btn-primary", method: :get) %>
      <% end %>
    <% end %>
  <% end %>
</div>