Reputation: 1120
I don't know much about ruby/rails. My app is currently producing some json to be consumed by message-bus:
message: act.as_json({
only: [:id, :body, :direction, :kind, :sent, :is_read, :created_at, :call_tag],
methods: [:delivering?, :attach_list, :voicemail_url, :status, :status_msg]
})
The data is coming back fine, the only problem is the created_at date is in an undesirable format: 2018-04-17 01:57:32 UTC
. I'd like it in iso8601: 2018-04-17T01:54:20.026Z
. In my controllers, jbuilder is being used and is returning json in the correct format. My search for a solution has led me to ActiveSupport, trying to somehow use jbuilder within this publish method of message-bus, to_json, etc. Is there some way to set created_at
to a custom value/format? If not, is there a reason why as_json is using this other format and jbuilder is using iso8601? Thanks
Upvotes: 0
Views: 2252
Reputation: 93
Rails 6 Override created_at everywhere
class ApplicationRecord < ActiveRecord::Base
def created_at
attributes['created_at'].strftime("%m/%d/%Y %H:%M")
end
end
Or localize
include ActionView::Helpers::TranslationHelper
#...
def created_at
l(attributes['created_at'], :format => "%d %B /%Y %H:%M")
end
#...
Upvotes: 1
Reputation: 437
You can override the created_at on the corresponding model
class Model < ActiveRecord::Base
def created_at
attributes['created_at'].strftime("%m/%d/%Y %H:%M")
end
end
And of course don't forget to adjust the format as you want
Then calling .created_at should return the values in the format you specified
Upvotes: 1