# Handling missing associations

**URL:** <https://www.ruby-forum.com/t/handling-missing-associations/177546>\
**Category:** Rails\
**Created:** [October 20, 2009, 1:01am UTC](https://www.ruby-forum.com/t/handling-missing-associations/177546 "2009-10-20T01:01:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [October 20, 2009, 1:01am UTC](https://www.ruby-forum.com/t/handling-missing-associations/177546/1 "2009-10-20T01:01:24Z")

</div>

Hello,

I have a message model which belongs to a user. Users can be deleted,  
but messages cannot. What can I do to reference a user that created a  
message when it no longer exists?:

1. Create a dummy user. When a user is deleted, assign the dummy user  
id to all of its messages.
2. Have a condition to check if the user exists, if not, don’t show  
the user info.
3. Don’t really delete the user, just flag it or change its state to  
inactive.

These 3 occured to me. Any other options I should have in  
consideration? Which one should I follow?

Thanks

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [October 20, 2009, 6:52am UTC](https://www.ruby-forum.com/t/handling-missing-associations/177546/2 "2009-10-20T06:52:50Z")

</div>

Hi Elias O.

```
  So do you want all messages be deleted when its user deleted?If so 

```

in user model you can add like (Assuming your association is user  
has\_many messages)

user.rb

has\_many :messages,:dependent =\> :destroy

```
 Now when you delete a user all the dependent messages will also get 

```

deleted

Sijo

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [October 20, 2009, 9:57am UTC](https://www.ruby-forum.com/t/handling-missing-associations/177546/3 "2009-10-20T09:57:14Z")

</div>

Elias O. wrote:

> Hello,
> 
> I have a message model which belongs to a user. Users can be deleted,  
> but messages cannot. What can I do to reference a user that created a  
> message when it no longer exists?:
> 
> 1. Create a dummy user. When a user is deleted, assign the dummy user  
> id to all of its messages.
> 2. Have a condition to check if the user exists, if not, don’t show  
> the user info.
> 3. Don’t really delete the user, just flag it or change its state to  
> inactive.
> 
> These 3 occured to me. Any other options I should have in  
> consideration? Which one should I follow?

I would use 3. 1 is not worth serious consideration (dummy records are  
seldom good things), and while 2 would work, it wouldn’t let you  
differentiate between messages from different inactive users.

> Thanks  
> Best,  
> –  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
