1

I have the following table structure for storing activity items

id, trackable_id, trackable_type, owner_id, owner_type

where trackable_id and trackable_type tells me on what object(entity) the action is performed on. For example if a User with id=1 likes a Picture with id=1

id, trackable_id, trackable_type, owner_id, owner_type

1,       1,            Picture,      1,        User

Similarly another user can like the same picture. In my news feed, I can easily display the activities performed by the users, however I would like to achieve something like this:

John and katy likes Picture.

instead of

John likes Picture
Kay  likes Picture.

I guess the way to go is to do a group by based on trackable_id and trackable_type but it returns me one row(expected result of group by). I have not used group_concat before, does it help in this kind of situation? Thanks!

Dinup Kandel
  • 2,457
  • 4
  • 21
  • 38
Hassan Javeed
  • 464
  • 4
  • 14

3 Answers3

2

Hey you can use Like this

CONCAT(GROUP_CONCAT( user_name SEPARATOR 'AND' ),' LIKES PICTURES')

Demo

Nisarg
  • 3,024
  • 5
  • 32
  • 54
0

Yes it helps.

Refer this

GROUP_CONCAT(columnName SEPARATOR ' AND ')

Community
  • 1
  • 1
Akhil
  • 2,602
  • 23
  • 36
0

Looks like you are using polymorfic associations (if no - then you should be using it). Your method will look like the following:

def liked_by_string
  user_names = users.collect(&:name).to_sentence # => "John, Katty, Mark and Pete"

  "#{user_names} like the Picture"      
end
Konstantin Rudy
  • 2,237
  • 25
  • 22