I have an table that includes these field:

table phonebook

id user_id number name added card_id speeddial updated_at sms_group_name

some records have same sms_group_name and number, but there is some duplicate number for same sms_group_name. First I want to take distinct number for each sms_group_name and group_concat with , delimiter.

query result must be like this:

I want to take result as a <ActiveRecord::Relation [#<Phonebook user_id: 5, number: "xxxxx,xxxxxx,xxxxxx", sms_group_name: "tem1">, #<Phonebook user_id: 5, number: "xxxxx,xxxxxx,xxxxxx", sms_group_name: "tem2">

I use dynamic where condition in my query ---> user_id.
select where condition is user_id

How can achieve that result?

solution approaches :
1- solution by using query { and may be added one block }
2- solution by using hash or array

I use mysql as a sqlserver

best regards