🌿 Issues

  • Option only_integer được dùng để validate chỉ nhận giá trị integer. Nhưng mình muốn nhận được cả giá trị float và integer cơ 😅

🌿 Solutuion

  • Theo doc, option only_integer validate format theo regex /\A[+-]?\d+\z.
  • Nếu muốn pass các giá trị float, có thể thay thế format regex /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/
validates :number, format: { with: /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/ }
  • Hoặc Rails 7 đã thêm option only_numeric để giải quyết vấn đề trên.
validates :number, numericality: { only_numeric: true }

🌿 Refer