I have fields hr and min, both integers in my application. For hr field, if the user enters “1” I would like Rails to automatically pad it to “01” before saving it to the database. Also for the min field if the user enter “0” it should put in as “00”.
How can I do this?
It’d be better to store it as an integer and just display it as you described on runtime. Every language has its own way to pad zeros – for Ruby you can use String#rjust. This method pads a string (right-justified) so that it becomes a given length, using a given padding character.