This 00012018
you want does not have to be persisted, and can be used for viewing only. To do this, you can implement a method in the template.
class Inscricao < ApplicationRecord
def numero_inscricao
return nil unless self.persisted?
id_com_zeros = "%04d" % self.id
"#{id_com_zeros}#{self.created_at.year}"
end
end
This will give you:
foo.numero_inscricao
=> "00012018"
As it uses #created_at
, it will work for the next years and for the records that are already in the database, because it is a function executed in memory.
Just be sure that the ID is an integer, not a UUID, as is possible.