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 tocompany
(companyhas_many :jobs
)Group (within company)
Company
has_many :groups
Location
@company = Company.find(params[:id])
@company.location