0 Replies - 981 Views - Last Post: 19 February 2018 - 09:40 AM Rate Topic: -----

#1 geministars   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-January 18

how to achieve query on ruby on rails

Posted 19 February 2018 - 09:40 AM

I have a table that includes these field:

table ----> phonebook

id user_id number name added card_id speeddial updated_at sms_group_name

records like these
1 3 33333333 tel1 2012-04-12 1 123 2012-04-12 group1
2 3 33333334 tel2 2012-04-12 2 124 2012-04-12 group1
3 3 33333333 tel3 2012-04-12 5 125 2012-04-12 group1
4 3 33333333 tel4 2012-04-12 4 126 2012-04-12 group1
5 3 33333344 tel5 2012-04-12 6 112 2012-04-12 group1
6 3 33333333 tel6 2012-04-12 7 143 2012-04-12 group2
7 3 33333223 tel7 2012-04-12 8 156 2012-04-12 group2
8 3 33333333 tel8 2012-04-12 9 546 2012-04-12 group2

my query result must be like that :

<ActiveRecord::Relation [#<Phonebook user_id: 3, number: "33333333,33333334,33333344", sms_group_name: "group1">, #<Phonebook user_id: 3, number: "33333333,33333223", sms_group_name: "group2">

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 only use to fetch results according to user_id in where condition
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 server

best regards

Is This A Good Question/Topic? 0
  • +

Page 1 of 1