age() function in postgres not working as expected
StanBright opened this issue · 2 comments
Hi,
First, thanks for the amazing gem/engine you've built. It's an indispensable part of all my projects. Kudos!
I'm using the latest version of blazer (2.6.5
as of now), and I'm trying to incorporate the age()
function from Postgres https://www.postgresql.org/docs/current/functions-datetime.html in some of my reports.
For example, showing the latest users and when they were created:
SELECT username, age(now() at time zone 'utc', created_at)
FROM users
ORDER BY created_at DESC
However, instead of date-time-in-words (e.g. "4 days 5 hours"), Blazer's UI returns/renders a float representing the age in seconds - an interval
in Postgres. At the same, if I execute the same query in the DB console directly, I'm getting the expected string.
I'm wondering, is that behaviour expected is a kind of bug/known-issue?
Thanks,
Stan
Hey @StanBright, thanks for reporting. Postgres use intervalstyle to determine how intervals are displayed. It defaults to postgres
, which is probably what you're seeing in the DB console.
It looks like Active Record added support for interval type in 6.1, which changes the style to iso_8601
and parses each value into an ActiveSupport::Duration
, which shows the number of seconds when to_s
is called.
I agree the current output isn't ideal - will try to spend some time on this in the future.
Amazing response. Thanks!