title: Best Practices For Creating a Notifications Database Table
body: <p>I'm stumped on how to best do this, and was hoping the community could shed some light on it (maybe you have experience, or have an innovative way of solving this!).</p>
  
  <p>I have an app where there are users, events, and groups. I'm trying to make a notifications table which will store notifications for users that they've been invited to events, groups, or connected with friends. No, I'm not building Facebook hah.</p>
  
  <p>The notification should have a message that says something like: "You've been invited to the Syracuse Runners Club" and you click it to jump to the Runners Club group object.</p>
  
  <p>So, my best guess so far is having a table structured as follows:</p>
  
  <p><strong>Notifications</strong></p>
  
  <pre><code>id | status (unread/read) | message | reference_object_id |
  </code></pre>
  
  <p>–where the reference object_id points to the id of the foreign object. However, the problem is that I won't know which table the foreign object_id belongs.</p>
  
  <p>My ultimate goal is to have this in JSON form so that I can have something along the lines of:</p>
  
  <pre><code>{ 
    message:"You're invited blah blah", 
    status:"unread", 
    reference_object: {
      id:1,
      name:"Runners Club"
    }
  }
  </code></pre>
  
  <p>I'm using MySQL and Ruby on Rails. </p>