Pregunta

So I have a Student model, and I am trying to design a page on my app that will display the full-time/internship job offers of all students in a table. I want the rows of the table to have:

  • Student Name
  • Company
  • Group (within company)
  • Location

Furthermore, I want logical horizontal divisions of the table -- I want the table first and foremost to be sorted by year, and within each year, I want it sorted by the type of job offer (either full-time or internship). Thus, theoretically, the JobOffer model that I want to make would have the six attributes listed above.

My question: What is the best logical way to actually implement this? I'm thinking of making my Student model has_many JobOffer's, and then using validates_inclusion_of to limit the values of the attribute job_type to "internship" and "full_time" in the JobOffer model. The student would then be able to create a new JobOffer for themselves; I would then collate all of the JobOffer items together for the JobOffer index page, where the table would be created.

Is there a better or more logical way to do this? Thanks.

¿Fue útil?

Solución

I'd use has_many :through & will treat JobOffer as a join model:


Models

#app/models/student.rb
Class Student < ActiveRecord::Base
    has_many :job_offers, class_name: "JobOffer"
    has_many :jobs, class_name: "Job", through: :job_offers
end

#app/models/job.rb
Class Job < ActiveRecord::Base
    belongs_to :company

    has_many :job_offers, class_name: "JobOffer"
    has_many :students, class_name: "Student", through: :offers
end

#app/models/job_offer.rb
Class JobOffer < ActiveRecord::Base
    belongs_to :student
    belongs_to :job
end

This will allow you to do this:

@student.job_offers # -> list of job offers
@student.jobs # -> applied jobs

@job.job_offers # -> list of offers from join model
@job.students # -> list of student who have applied for a job

Schemas

students
id | name | email | etc | etc | created_at | updated_at

jobs 
id | company_id | name | type | information | group | location | created_at | updated_at

job_offers
id | student_id | job_id | extra | vars | created_at | updated_at

Controllers

#app/controllers/job_offers_controller.rb
def new
    @job_offer = JobOffer.new
end

def create
    @job_offer = JobOffer.new(job_offer_params)
    @job_offer.save
end

private
def job_offer_params
    params.require(:job_offer).permit(:extra, :vars).merge(student_id: current_user.id, job_id: params[:id])
end


#app/controllers/students_controller.rb
def profile
    @student = Student.find(current_user.id)
    @job_offers = @student.job_offers
    @jobs = @student.jobs
end

Views

#app/views/job_offers/new.html.erb
<%= form_for @job_offer do |f| %>
    <%= f.text_field :extra_var_1 %>
    <%= f.text_field :extra_var_2 %>

    # -> student_id & job_id appended automatically
<% end %>

Routes

#config/routes.rb
resources :jobs do
    resources :job_offers, as: 'offers'
end

This will give you the routes: /jobs/1/offers/new


Notes

  • Student Name

    Can be called from:

    @job.students.each do |student| student.name end

    or

    @student_names = @job.students.map(&:name) #-> ["Joe", "Mary", "Henry"]

  • Company

    Job belongs to company (company has_many :jobs)

  • Group (within company) Company has_many :groups

  • Location

    @company = Company.find(params[:id]) @company.location

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top