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 8167163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:07:01+00:00 2026-06-06T20:07:01+00:00

I have an Rails ActiveRecord group query: a = Product.select(date(date) as date, count(id) as

  • 0

I have an Rails ActiveRecord group query:

a = Product.select("date(date) as date, count(id) as products").group("date(date)").to_a 

and I want to change the format of all of the dates in the array. I thought it would be something like this:

a.map { |s, i| [Date.parse(s).to_time.strftime("%Y-%m-%dT%H:%M:%S"), i] }

However, I am getting a can't convert Product into String error. How can I parse the dates in my array and change their format?

EDIT:

Here is an example of the output I am looking for.

Given this table:

 create_table :products do |t|
       t.date :date
 end

Ultimately, I want to create a json output based on the above group query. Something like:

 [{"products":23,"date":"2012-06-15T00:00:00"}, {"products":26,"date":"2012-06-16T00:00:00"}] 
  • 1 1 Answer
  • 0 Views
  • 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-06-06T20:07:02+00:00Added an answer on June 6, 2026 at 8:07 pm

    Try this:

    a.map { |s, i| [Date.parse(s.date).to_time.strftime("%Y-%m-%dT%H:%M:%S"), i] }
    

    Your query returns an array of Product objects with two fake fields date and products (same as in the SQL query). To get dates you should call s.date on the elements of the returned array.

    EDIT

    To get desired array you can do:

    query_result = Product.select("date(date) as date, count(id) as products").group("date(date)")
    
    query_result.map do |row| 
      { :products => row.products, :date => Date.parse(row.date).to_time.strftime("%Y-%m-%dT%H:%M:%S") }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails 2.1 app with order which belongs to group. I want
I'm working with an older version of Rails and ActiveRecord that does not have
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
In a Rails app I have Sales and Salespeople: class Sale < ActiveRecord::Base belongs_to
I have a non-rails application that I want to use rails active-record migrations with.
My Rails application have a User model and a Group model, where User belongs
In my Rails app, I have the following PostgreSQL query to find the most
What is the Rails 3 way to order .group() results in Activerecord (here by
(Using Rails 3.1.3) I have an app that manages products. I import the products
Is it possible to group by distance using the geokit-rails gem for ActiveRecord? Say

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.