The response will depend on the database you use.
Postgresql
For postgres, you can do something like this :
@notifications = current_user.notifications.
select( "string_agg( users.user_name, ', ' ) as user_names, notifiables.name as notifiable_name" ).
group( 'notifiables.name, notifications.notifiable_type' ).
joins( :notifiable ).joins( :user ).
page(params[:page]).per(10)
Then, use the aggrated object like that :
@notifications.map { |n| "#{n.user_names} #{n.notifiable_type} #{n.notifable_name}" }
Note that I have guessed you other tables field names, so adapt accordingly.
The basic idea is to group your results and use database concatenation features. Kaminari and will_paginate will work happily with the result.
Beware : as always when you use #select
, do not write objects back to database or they'll be corrupted.
MySql
For mysql, this should be basically the same, but replacing string_agg( users.user_name, ', ' )
with group_concat( users.user_name )