【问题标题】:Notifications or user activity log implementation通知或用户活动日志实现
【发布时间】:2023-03-13 23:47:02
【问题描述】:

应用程序具有为每个用户记录并记录在数据库表notifications 中的某些操作。该表的结构如下:

user_id, notification_type, credit, timestamp

notification_type 不存储通知的整个文本,而仅存储简短的类型描述。

稍后当用户想要查看他的通知时,我会使用视图中的辅助方法来获取实际文本。

    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) 这是执行此操作的最佳方式吗?

2) 是否应该将 type_description 替换为整数值(例如 1 -> flagPositive2-> qAccepted)以获得性能优势?

3) 是否有任何我应该遵循的最佳实践?

【问题讨论】:

    标签: ruby-on-rails notifications


    【解决方案1】:

    1) 这在很大程度上取决于您的应用程序和要求。我可以说的是,我有时使用这种方法,到目前为止没有遇到任何问题。

    2) 如果您发现字符串查找存在性能问题,您可以这样做。一般建议是仅在真正需要时优化性能。

    3) 只需在谷歌上搜索“Ruby”、“Rails”、“ActiveRecord”和“Enum”。您会发现很多关于此类问题的不同解决方案的讨论。本站有类似问题,如Enums in RubyIn Rails, how should I implement a Status field for a Tasks app - integer or enum?

    【讨论】:

      猜你喜欢
      • 2019-03-17
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2015-04-04
      相关资源
      最近更新 更多