Keeping a record that belonged to a record that has been del

I have 2 tables. One for employees, the other for testimonials. An
employee has_many testimonials and a testimonial belongs to an employee.

Now when an employee leaves, we as a company want to keep the
testimonial, but just not have it assigned to an employee. Problem is,
if the employee is deleted form the table, and a testimonial still
references that employee via employee_id, errors get thrown up.

What would be a good solution for this? I thought about creating a blank
employee with an id of 0. But that too could get deleted through the
system. Is there a way to tell ruby a certain employee record is read
only and can’t be deleted without having to write the restrict code over
and over? Or maybe there is a way to creata a fake, static employee in
the model?

Thanks
Jeremy

could you do this?

class Employee < ActiveRecord::Base
has_many (or has_one) :testimonials, :dependent => :nullify
end

This will set the employee_id field to NULL in the testimonial table.

jko170 wrote:

could you do this?

class Employee < ActiveRecord::Base
has_many (or has_one) :testimonials, :dependent => :nullify
end

This will set the employee_id field to NULL in the testimonial table.

From the API:
If set to :nullify all associated objects’ foreign keys are set to NULL
without calling their save callbacks.

So wouldn’t that nulify every employee_id of the record in the
testimonial table? Wouldn’t that make is so I can’t tell what employee a
testimonial is assigned to?

The “nullify” is only set on the models associated with the deleted
model, not all of the records in the associated table.

And just a thought:
Why delete the employee?
Why not use some sort of status to indicate they are no longer
currently employed?

You can then handle the display of employees based on status, and can
easily collect testimonials from all employees who are no longer
current.

Just a thought.

Toby Hede
Web Application Development
Melbourne, Australia

On Aug 10, 9:07 am, Jeremy L. [email protected]

tobyhede wrote:

The “nullify” is only set on the models associated with the deleted
model, not all of the records in the associated table.

OK that makes a lot more sense. Thanks, It works.

And just a thought:
Why delete the employee?
Why not use some sort of status to indicate they are no longer
currently employed?

There is a bool for active or not which defines if they stay, but this
is in an industry with very high turnover, so they have to be deleted
for organizational reasons.

Thanks
Jeremy