How to generate sequential numbers automatically with current year in rails?

0

I need when a new product is created it generates a number ex: 0001/2015 number / year_vigente and when change of year returns to zero.

I made a helper just to format the number in the views, but it does not solve the problem of automatic generation and renewal next year.

def format_product_number(product_code)
 format_product = product_code[0..4]
 format_product << "/#{Time.now.year}"
 format_product
end 

I have a form with a field:

@product

<%= f.label :product_number %>
<%= f.number_field :product_number %>

Does anyone know a way to do this?

    
asked by anonymous 15.05.2015 / 21:26

1 answer

1

@AlexTakitani has made good inquiries, but to respond quickly you can store this in the bank using a callback :

    before_create :record_code_number

    private

    def record_code_number
      self.your_field_table = "#{product_code[0..4]}/#{Time.now.year}"
    end

Always look for the type of callback used, and for the validations too!

Thanks!

    
22.05.2015 / 23:25