Rails 7: How to handle time zone implementing ActiveRecord timestamps during API responses?
Hey everyone, I'm running into an issue that's driving me crazy. I'm experiencing a question with handling time zones in my Rails 7 API. When I create records, they are saved in UTC, which is expected. However, when I retrieve these records, the timestamps are returned without any conversion to the client's time zone. I would like to ensure that the timestamps sent in the API response reflect the client's local time zone instead of UTC. I have the following setup in my `application.rb`: ```ruby config.time_zone = 'UTC' config.active_record.default_timezone = :utc ``` In my controller, I'm trying to convert the timestamp using `Time.zone.at` before sending the response: ```ruby def show record = MyModel.find(params[:id]) render json: record.as_json(methods: [:local_created_at]) end private def local_created_at created_at.in_time_zone(current_user.time_zone || 'UTC') end ``` Despite this approach, the timestamps are still showing in UTC in my API response. I've also tried using the `active_model_serializers` gem to handle serialization and set up an attribute for `created_at` but that didn't seem to resolve the scenario either. Here's a snippet of my serializer: ```ruby class MyModelSerializer < ActiveModel::Serializer attributes :id, :local_created_at def local_created_at object.created_at.in_time_zone(@instance_options[:time_zone]) end end ``` When I call my API endpoint, I see timestamps like this: ```json { "id": 1, "local_created_at": "2023-10-01T12:00:00Z" } ``` I'm not sure if I'm missing something regarding Rails time zone configuration or if I need to explicitly set the time zone in the controller or serializer. Any guidance on ensuring timestamps are sent in the client's time zone would be greatly appreciated. Am I missing something obvious?