How to convert input hours, minutes, seconds
In my model, I have a "duration" attribute (in seconds as an integer). But new / edit views have input for: hours, minutes, seconds.
Should I have (attr_accessor: hours ,: minutes ,: seconds) in the model, how can I convert these virtual attributes to my "duration" attribute? Arriving from Java, I would set / getter to "duration"!
+2
source to share
1 answer
You can set a before_save filter to calculate its value from the values โโof your virtual accessories (hours, minutes and seconds). Something like:
class Model << AR:Base
attr_accessor :hours, :minutes, :seconds
before_save :update_duration
def update_duration
duration = = hours*3600 + minutes*60 + secods
end
end
It's a little rough, but should give you an idea of โโhow it might work. Whenever you call the Save method on the model (or when creating or updating actions), the filter will be called before accessing the BD, updating it, and then proceeding correctly.
+6
source to share