Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
Best way to convert a field in Rails from decimals to integer in cents

I have a Product model in Rails and the amount is stored as an integer in cents in the database.

Example of this field:

enter image description here

  • What is the best way to allow admins that are creating the product to enter a human-friendly decimal like 10.00 instead of 1000?
  • Should I just do this on the client-side before the form is submitted with Javascript?
  • Should I change the field to a text field instead of integer and convert it with a before_save callback?

It seems like the Money gem might be overkill for this. Surely this is a common problem. What is a common way people solve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just as ideas

Using money gem

class Product < ApplicationRecord
  monetize :price_cents
end
= f.input :price

Or using monkeypatching

class Product < ApplicationRecord
  def price
    self[:price] / 100.0
  end

  def price=(value)
    super(value * 100)
  end
end

In my opinion gem is more convenient

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda