Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 832545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:22:43+00:00 2026-05-15T04:22:43+00:00

I have objects that have comments. As part of a periodic email summary, I

  • 0

I have objects that have comments. As part of a periodic email summary, I want to determine comments for a period, and present the objects in the order of oldest commented object first.

Data:

object_id  comment_id
30         40
40         42
32         41
30         43
32         44

Output:

Object #30

  • comment 40
  • comment 43

Object #40

  • comment 42

Object #32

  • comment 41
  • comment 44

I am using this code to get the data to an intermediate array – I tried to get it all in one swoop using .group_by(&:commentable_id) but the data didn’t come out in correct order.

comments = account.comments.all( 
  :conditions => ["comments.created_at > ?", 8.hours.ago],
  :order => "comments.created_at asc" ).map { |c| [c.commentable_id,c.id] }

=>  [ [30,40], [40,42], [32,41], [30,43], [32,44] ]

If I can get that data to transform into the following form, I could just iterate over the array to build the email content…

[ [30,[40,43]], [40,[42]], [32,[41,44]] ]

But I wonder if I’m making this harder than I need to… Any advice?

(I’m using Rails 2.3 and Ruby ree-1.8.7)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:22:43+00:00Added an answer on May 15, 2026 at 4:22 am

    You can use a group with an array aggregate to get to the array form that you’re looking for.

    Array aggregates are massively db dependent. MySQL’s is GROUP_CONCAT. Postgres’ is ARRAY_AGG. Sqlite doesn’t have one out of the box, but I know you can define custom aggregate functions, so it’s not impossible.

    Haven’t actually tried running this code, but here’s something that should point you in the right direction:

    result = Object.all(
      :select => 'objects.id, GROUP_CONCAT(comment_id) AS comment_array',
      :group => 'comments.id'
    ).map { |c| [c.id, c.comment_array] }
    

    I used the naming from the first example, so you’ll need to change ‘object’ to whatever your table is called. Hope it makes sense. Rails probably doesn’t have inbuilt support for parsing an array, so it will probably return a string for comment_array, and you might have to parse it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have objects with many variables that I declare and explain in the comments.
We have objects that we want to represent in stacks (think of stacking items
I use the django comments from contrib and I have an object (entry) that
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have objects that has a DateTime property, how can i query for the
I have Process objects that are monitored from two different views. A Windows.Forms.ListView (actually
I have two objects that can be represented as an int, float, bool, or
I have managed objects that have image properties. Since storing large blobs in CoreData
I'm looking for ideas/suggestions on a namespace. I have 3 objects that do the
I have an array of objects that have QTTime structs as attributes. The objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.