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

**URL:** <https://www.ruby-forum.com/t/keeping-a-record-that-belonged-to-a-record-that-has-been-del/111705>\
**Category:** Rails\
**Created:** [August 9, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/keeping-a-record-that-belonged-to-a-record-that-has-been-del/111705 "2007-08-09T23:16:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eboss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea666f/32.png) [@eboss](https://www.ruby-forum.com/u/eboss)\
**Post date:** [August 9, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/keeping-a-record-that-belonged-to-a-record-that-has-been-del/111705/1 "2007-08-09T23:16:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eboss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea666f/32.png) [@eboss](https://www.ruby-forum.com/u/eboss)\
**Post date:** [August 9, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/keeping-a-record-that-belonged-to-a-record-that-has-been-del/111705/2 "2007-08-09T23:56:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eboss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea666f/32.png) [@eboss](https://www.ruby-forum.com/u/eboss)\
**Post date:** [August 10, 2007, 1:07am UTC](https://www.ruby-forum.com/t/keeping-a-record-that-belonged-to-a-record-that-has-been-del/111705/3 "2007-08-10T01:07:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![eboss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea666f/32.png) [@eboss](https://www.ruby-forum.com/u/eboss)\
**Post date:** [August 10, 2007, 2:01am UTC](https://www.ruby-forum.com/t/keeping-a-record-that-belonged-to-a-record-that-has-been-del/111705/4 "2007-08-10T02:01:20Z")

</div>

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

> **[Toby Hede](https://tobyhede.com/)**
>
> Toby Hede

On Aug 10, 9:07 am, Jeremy L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eboss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea666f/32.png) [@eboss](https://www.ruby-forum.com/u/eboss)\
**Post date:** [August 10, 2007, 2:56am UTC](https://www.ruby-forum.com/t/keeping-a-record-that-belonged-to-a-record-that-has-been-del/111705/5 "2007-08-10T02:56:08Z")

</div>

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
