# Seeking some advice

**URL:** <https://www.ruby-forum.com/t/seeking-some-advice/104037>\
**Category:** Rails\
**Created:** [May 13, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/seeking-some-advice/104037 "2007-05-13T15:14:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 13, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/seeking-some-advice/104037/1 "2007-05-13T15:14:35Z")

</div>

Sorry for the vague subject. I couldn’t think of a more descriptive one.

I’m working on an app in which users are able to send one another  
messages. I’d like for each user to have a sent folder, as well as an  
inbox. A user’s inbox should show who the message came from, and the  
sent folder should show to whom it went. I want to only store one copy  
of each message, and let the sender and receiver “delete” the message  
independently of one another. My first stab was this (non-relevant  
code omitted):

class Member \< ActiveRecord::Base  
has\_many :mail\_messages, :dependent =\> :nullify  
has\_many :sent\_messages, :class\_name =\> ‘MailMessage’,  
:foreign\_key =\> :sender\_id, :dependent =\> :nullify  
end

class MailMessage \< ActiveRecord::Base  
belongs\_to :member  
belongs\_to :sender, :class\_name =\> ‘Member’, :foreign\_key =\>  
‘sender\_id’  
end

This works exactly as it should. When a user is destroyed, the  
sender\_id or member\_id field for each message related to them is  
nullified appropriately. The plan was to have a reaper run  
periodically to delete rows from the mail\_messages table where both  
member\_id and sender\_id are null (in other words, both the sender and  
receiver have deleted the message from their respective views).

There’s a fly in this ointment, though. If the sender is destroyed,  
the sender\_id field of the message is nullified, and the receiver’s  
view can no longer show who the message came from. Likewise for the  
sender if the receiver is destroyed.

The only solution I’ve thought of is to add two more fields to the  
mail\_messages table to store the sender’s and receiver’s ID - not for  
purposes of associating the mail\_message record to the members, but  
just to be able to show who the sender and recipient are in UI views.  
This strikes me as unclean, though. I’m not crazy about the idea of  
duplicating information in a row. I’m hoping someone out there has a  
better idea I’m not thinking of?

Thanks . . .

–  
Bill K.

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 13, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/seeking-some-advice/104037/2 "2007-05-13T15:23:40Z")

</div>

On 5/13/07, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The only solution I’ve thought of is to add two more fields to the  
> mail\_messages table to store the sender’s and receiver’s ID - not for  
> purposes of associating the mail\_message record to the members, but  
> just to be able to show who the sender and recipient are in UI views.

I love embarrassing myself in public forums. After thinking about this  
since sometime yesterday and deciding to seek advice, it finally  
struck me after I sent this message: even if I do duplicate the sender  
and receiver ID in the mail\_messages rows, once either of them are  
destroyed their ID becomes a meaningless number from which no  
information can be gleaned. So my options are, I guess, to store their  
display name (i.e., “Bob” instead of “45398”) for the UI views, or  
simply show messages to/from deleted members as being to/from an  
account that no longer exists (a la MySpace).

Thanks for listening to me think out loud, everyone. You’ve been very  
helpful. 🙂

–  
Bill K.

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 13, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/seeking-some-advice/104037/3 "2007-05-13T18:26:54Z")

</div>

Or rather than deleting the user, just flag the user as “inactive”.
