Validating decimal scale in Ruby on Rails

NasEscobar

I'm new to Ruby on Rails. I have the following model:

class Logo < ApplicationRecord
  validates :name, presence: true,  length: { maximum: 255 }
  validates :price, presence: true, 
    numericality: { greater_than_or_equal_to: 0 }

  validates :stitch_count, presence: true,
    numericality: { greater_than: 0, only_integer: true }
end

Every logo has a price. I want to validate the scale of the price. It must accept the following: [10.44, 12.1, 16] and reject the following: [10.123, 13.11111, 16.123], as these are obviously not valid prices. I've tried validating by format using the following regex: /\A\d+(?:\.\d{0,2})?\z/, which, according to another poster on Stack Overflow, validates for at most 2 decimal places. It doesn't work. Another poster on Stack Overflow pointed out that the number of decimal places only matters when the value is a string, and that by the time the validator runs, it's no longer a string. His suggestions, as well as a few more, don't work. So how would I run a regex on a decimal? You would think Rails would have something to validate if a number is a valid price or not.

I run the following test:

  def setup
    @logo = Logo.new(name: "ASG Security", price: 12, stitch_count: 15000, note: "Ali")
  end

  test "price scale must be less than 2" do
    @logo.price = 14.995
    assert_not @logo.valid?
  end
chad_

You could use the money gem, which I've used, or maybe try this rails wrapper for it which provides premade money validations.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ruby/Rails - How can you validate against decimal scale?

From Dev

rails decimal scale => 2 not working

From Dev

Ruby/Rails Percentage to decimal

From Dev

Validating with ActiveModel in Ruby on Rails 4 Seemingly not Working

From Dev

ruby rails remove scale and precision from database

From Dev

Get data back after validating a Coupon with Ruby on Rails using AJAX

From Dev

Ruby on Rails: proper method of validating Model data comparing with parameter(s)

From Dev

Ruby on Rails Migration Convert From String To Decimal

From Dev

Ruby Rails: wont save as decimal in postgresql

From Dev

validating decimal numbers in culture

From Dev

Rails decimal (currency) number not saving when the scale part changes

From Dev

Rails decimal (currency) number not saving when the scale part changes

From Dev

MVC Jquery globalization - validating a decimal

From Dev

Validating user input of decimal number

From Dev

Validating associated models in Rails

From Dev

Rails validating values in array

From Dev

Rails: Validating an array of ids

From Dev

Validating minutes and seconds in Rails

From Dev

Rails validating enums

From Dev

Decimal precision and scale

From Dev

entity framework decimal scale

From Dev

Decimal conversion scale errors

From Dev

Validating OpenCart password hash in ruby

From Dev

Validating OpenCart password hash in ruby

From Dev

Validating ruby installation after install?

From Dev

Aspnet MVC 4 validating decimal numbers

From Dev

Javascript/Jquery validating decimal input on keyup

From Dev

Aspnet MVC 4 validating decimal numbers

From Dev

Regex for validating decimal number with fixed range

Related Related

HotTag

Archive