Answer the question
In order to leave comments, you need to log in
How to change attribute type in after_find in Rails 4?
There is a table with video. The duration is stored in seconds, but I want to give the output in a readable format "00:05:23". Now duration will get 0. How to drive a string there?
class Video < ActiveRecord::Base
validates_numericality_of :duration
after_find :format_duration
protected
def format_duration
self.duration = Time.at(self.duration).gmtime.strftime('%R:%S')
end
end
Answer the question
In order to leave comments, you need to log in
Why not write a helper that will do it?
def formated_duration(duration)
Time.at(duration).gmtime.strftime('%R:%S')
end
<%= formated_duration(video.duration) %>
Apparently, it can only be done using virtual attributes and attr_accessor.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question