Handling missing associations

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

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

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
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs