Varun Jain
Varun Jain

Reputation: 1911

Notifications or user activity log implementation

The application has certain actions that are recorded for each user and recorded in db table notifications. The table has the following structure:

user_id, notification_type, credit, timestamp

notification_type does not store the entire text of the notification but just stores a short type description.

Later when the user wants to view his notifications I use a helper method from my view to fetch the actual text.

    def notification_text(type)
     case type_id
             when 'flagPositive'
                 return 'A question you flagged has been marked as correct.'
             when 'qAccepted'
                 return 'A question you added has been accepted.'

             when 'qModerated'
                  return 'You moderated a question.'
             when 'flagReport'
                  return 'You moderated a flag.'
           end
 end

1) Is this an optimum way to do this?

2) Should I replace the type_description with integer values (say 1 -> flagPositive, 2-> qAccepted) for performance benefits?

3) Are there any best practices around the same that I should be following?

Upvotes: 0

Views: 198

Answers (1)

claasz
claasz

Reputation: 2134

1) This highly depends on your application and requirements. What I can say is that I used this approach sometimes and faced no problems so far.

2) If you see a performance problem with the string lookup, you could do so. A general recommendation is to optimize performance only when really needed.

3) Just google for "Ruby", "Rails", "ActiveRecord" and "Enum". You'll find lots of discussions about different solutions for this kind of problem. There are similar questions on this site, e.g., Enums in Ruby or In Rails, how should I implement a Status field for a Tasks app - integer or enum?

Upvotes: 1

Related Questions