frustrateddeveloper
frustrateddeveloper

Reputation: 35

Rails => Has_many and belongs_to ...

I'm making a jobs board app on Ruby on Rails.

I have 2 models, Job and Resume... Job has

class Job < ActiveRecord::Base
  has_many :resumes
end

and Resume

class Resume < ActiveRecord::Base
  belongs_to :job
end

What I want It's the ability to eliminated a job posting without eliminating the association with the Resume, because All resumes have a Job associated and in the future I want to remember witch resume belongs_to an Old job posting...

I'm using Rails_admin so that's why I want to remember witch resume belongs to a job

Upvotes: 1

Views: 246

Answers (2)

Sean Marzug-McCarthy
Sean Marzug-McCarthy

Reputation: 86

Expanding on Adnan's answer, you could add a boolean column to your Job model called active that defaults to true. When you want to "delete" a job, just toggle the field to false. On the user-facing side, you can scope the jobs so that only the active ones are shown.

class Job < ActiveRecord::Base
  has_many :resumes

  scope :active, -> { where(active: true) }
end

class JobsController < ApplicationController
  def index
    @jobs = Job.active
  end
end

Upvotes: 0

Adnan
Adnan

Reputation: 2967

Maybe rather than delete a job, use some kind of deactivation flag on the Job model to "eliminate" it. That way you can preserve all associations even after eliminating a job.

Upvotes: 1

Related Questions