I currently have a notifications table in rails which contains the columns: “notification_type”, “notification_id”
Where the notification type is either “Post”, “Friend”, “Rating”. I have separate tables called the same names as the notification type.
What I want to do is be able to use the notification_type and ID to get the correct data from the other tables. ie A row with a notification_type of “Post” and an ID of 1 should get the the data from the Post table where the id is 1. (However, I want to get more than just one row from Post if there are more rows with “Post” notification_type).
I also need to combine all the data retrieve from the three tables together so I can have something like:
@all_notifications = ...
And call something like this from my view:
@all_notifications.each do |notification|
if notification.type == "Post"
<%= notification.post_content %>
end
end
Alternatively, I’m open for a better suggestion on how to organise my tables.
Thanks in advance
So far it sounds like you want polymorphic associations.