I need to convert all dates that get returned by my api into Unix date format (seconds). Individually is easy enough...
class ChimichangaSerializer < ActiveModel::Serializer
attributes :updated_at,
def updated_at
object.updated_at.to_i
end
end
But since I have to do it for everything, that way lies errors and madness. How can I achieve this functionality for all of them?
Add the following:
app/config/initializers/time_with_zone.rb
class ActiveSupport::TimeWithZone
def as_json(options = {})
to_i
end
end
this will override the default behaviour for all your timestamps when converted into json.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With