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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:23:15+00:00 2026-06-13T10:23:15+00:00

I have send json response of a Rails AREL query result based on two

  • 0

I have send json response of a Rails AREL query result based on two tables. The response includes two timestamp columns: updated_at and updated_at_table_2. I override ActiveSupport::TimeWithZone.as_json method to obtain the desired datetime format for JSON. The same approach is shown at

http://stackoverflow.com/questions/2937740/rails-dates-with-json

My current JSON response is:
{
“updated_at”:”10/26/2012 22:04:07 -0400″,
“updated_at_table_2″:”2012-10-27 02:04:07.463015”
}

I want them to be the same. I relies below Rails code to produce json.

render :json => { :customer_order  =>  @customer_order  }

where @custoemr_order derived from:

CustomerOrder.select(%Q[
      updated_at,
      c.updated_at as updated_at_table_2
    ] ).
    joins( %{ as co inner join customers as c on (co.customer_id = c.id)

Question: how can I tell Rails 3 to process as_json the same way for updated_at_table_2 as for updated_at column?

Any suggestions/pointers are great too.

Note: I found this post asking about the same root problem (though not about json) without good solution. :

http://stackoverflow.com/questions/12067173/rails-postgres-not-returning-timezone-info-on-column-from-joined-table
  • 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-13T10:23:16+00:00Added an answer on June 13, 2026 at 10:23 am

    If you look at what your query gives you, you’ll see exactly where you’re going wrong:

     > o = CustomerOrder.select(%Q[ ... ]).joins(...)
     > puts o.updated_at.class
    => ActiveSupport::TimeWithZone
     > puts o.updated_at_table_2.class
    => String
    

    ActiveRecord has no way of know what sort of thing updated_at_table_2 is supposed to be so it leaves it as a String and your to_json monkey patch won’t be applied to strings.

    If you want to keep using your to_json monkey patch (which I think is a bad idea), then you need to manually convert updated_at_table_2 to an ActiveSupport::TimeWithZone with something like this:

    x = ActiveSupport::TimeWithZone.new(
        Time.parse(o.updated_at_table_2),
        Time.zone
    )
    # x is now an ActiveSupport::TimeWithZone
    

    Alternatively, you could load the associated object and call updated_at on that.

    I’d scrap the whole approach though: let the server work exclusively in UTC, send the clients timestamps in an ISO-8601 format, and let the clients deal with applying the local timezone.

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

Sidebar

Related Questions

I have a webservice url that retrieve a JSON as response.I need to send
I have to send three asynch requests in three class files, 3 requests response
I have to send .csv file to remote server through REST in rails. I
I have a small problem parsing json response because it constantly keeps on getting
I have the following json: { response: { status: 200 }, user: { flex_meta:
I have send an email using php mailer class. mail was sending successfully but
I am using Lotus Notes Java API for my application. I have send one
I have to send a request as https post to a third party Apache
I have to send a large amount of xml data through sockets. Example of
I have to send a long string of data through webservice to database.I odnt

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.