Ruby / Rails - How to aggregate a Query Results in an Array?

Posted by AnApprentice on Stack Overflow See other posts from Stack Overflow or by AnApprentice
Published on 2011-02-11T23:02:42Z Indexed on 2011/02/11 23:25 UTC
Read the original article Hit count: 414

Hello, I have a large data set that I want to clean up for the user. The data set from the DB looks something like this:

ID | project_id | thread_id | action_type |description
 1 | 10         | 30        |  comment    | yada yada yada yada yada
 1 | 10         | 30        |  comment    | xxx
 1 | 10         | 30        |  comment    | yada 313133
 1 | 10         | 33        |  comment    | fdsdfsdfsdfsdfs
 1 | 10         | 33        |  comment    | yada yada yada yada yada
 1 | 10         |           | attachment  | fddgaasddsadasdsadsa
 1 | 10         |           | attachment  | xcvcvxcvxcvxxcvcvxxcv

Right now, when I output the above in my view its in the very same order as above, problem is it is very repetitive. For example, for project_id 10 & thread_id 30 you see:

10 - 30 - yada yada yada yada yada
10 - 30 - xxxxx
10 - 30 - yada yada yada yada yada

What I would like to learn how to do in ruby, is some how create an array and aggreate descriptions under a project_id and thread_id, so instead the output is:

10 - 30
 - yada yada yada yada yada
 - xxxxx
 - yada yada yada yada yada

Any advice on where to get started? This requirement is new for me so I would appreciate your thoughts on what you're thinking the best way to solve this is.Hopefully this can be done in ruby and not sql, as the activity feed is likely going to grow in event types and complexity.

Thanks

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby