Victor Castillo Torres Victor Castillo Torres - 6 months ago 20
Ruby Question

Rails returning UTC instead of just text with Postgresql

So I'm working with Rails (4.2.6) and PostgreSQL, I have a table which contains a field named

of type Time, the problem I have is the next:

SELECT end_time FROM group_periods;

Will result in:

| end_time |
| 08:00:00 |

As you can see it only brings the text that is contained in the field (
) but if I try to do It in Rails it will bring in
a Time Object even if I cast it to text"end_time")

Will result in:

=> #<ActiveRecord::Relation [#<GroupPeriod id:nil, end_time: "2000-01-01 08:00:00">]>

after I try to access to
object it brings a Time object for Ruby.

I also have tried, resulting in the same:"end_time::text")"to_char(end_time, 'HH24:MI') end_time")

How could I get the field
as a string so whenever I access the field I get the same result as Postgres?


There is no chance to do a strftime and get te string format on every record due to I have thousands of records, that's the main reason I have already tried to cast the field as a text so rails would return a string instead of a Time object.


You could override the attribute accessor for end_time on the model and return the attribute before type casting

Something like

class GroupPeriod < ActiveRecord::Base

  def end_time