So, I did something a bit before the second answer was sent, but too busy to compose and put it here. And I'm still studying if I did the right thing here, if it scaled, or how it would work in general. I would like to hear all your ideas, suggestions, comments on how I did this. Here:
Therefore, I first created the tables as typical polymorphic tables.
# migration create_table :activities do |t| t.references :receiver t.references :user t.references :notifiable t.string :notifiable_type
The way it is. Then the insertion is pretty typical, for example, when the user performs a certain type of notification. So, what's new that I found for psql is ARRAY_AGG , which is an aggregated function that groups a specific field into a new field as an array (but a string when it comes to the rails). So, as I get the entries, now like this:
# notification.rb scope :aggregated, select('type, notifiable_id, notifiable_type, DATE(notifications.created_at), COUNT(notifications.id) AS count, ARRAY_AGG(users.name) AS user_names, ARRAY_AGG(users.image) as user_images, ARRAY_AGG(id) as ids'). group('type, notifiable_id, notifiable_type, DATE(notifications.created_at)'). order('DATE(notifications.created_at) DESC'). joins(:user)
This outputs something like:
type | notifiable_id | notifiable_type | date | count | user_names | user_images | id "Comment"| 3 | "Status" |[date]| 3 | "['first', 'second', 'third']" |"['path1', 'path2', 'path3']" |"[1, 2, 3]"
And again, in my notification model, I have methods that basically just return them to an array and delete non-uniques (so that the name will not appear twice in a specific aggregated notification):
# notification.rb def array_of_aggregated_users self.user_names[1..-2].split(',').uniq end def array_of_aggregated_user_images self.user_images[1..-2].split(',').uniq end
Then, in my opinion, I have something like this
# index.html.erb <% @aggregated_notifications.each do |agg_notif| %> <% all_names = agg_notif.array_of_aggregated_users all_images = agg_notif.array_of_aggregated_user_images %> <img src="<%= all_images[0] %>" /> <% if all_names.length == 1 %> <%= all_names[0] %> <% elsif all_names.length == 2 %> <%= all_names[0] %> and <%= all_names[1] %> <% elsif all_names.length == 3 %> <%= all_names[0] %>, <%= all_names[1] %> and <%= all_names[2] %> <% else %> <%= all_names[0] %> and <%= all_names.length - 1 %> others <% end %> <%= agg_notif.type %> on your <%= agg_notif.notifiable_type %> <% if agg_notif.count > 1 %> <%= set_collapsible_link # [-/+] %> <% else %> <%= set_actions(ids[0]) %> <% end %> <% end %>