Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 677639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:03:41+00:00 2026-05-14T01:03:41+00:00

I was trying to write a validation for Rails to ensure that a price

  • 0

I was trying to write a validation for Rails to ensure that a price entered on a form was greater than zero. It works…sort of. The problem is that when I run it, val is turned into an integer, so it thinks that .99 is less than .1. What’s going on, and how should I fix the code?

class Product < ActiveRecord::Base
  protected
    def self.validates_greater_than_zero(*attr_names)
      validates_each(attr_names) do |record, attr, val|
        record.errors.add(attr, "should be at least 0.01 (current val = #{val.to_f})") if val.nil? || val < 0.01
      end
    end
  public
  validates_presence_of :title, :description, :image_url
  validates_numericality_of :price
  validates_greater_than_zero :price
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:03:42+00:00Added an answer on May 14, 2026 at 1:03 am

    If the original string value is cast to an integer, it will be rounded down. So “0.99” gets rounded down to 0, which is obviously less than 0.01. You should compare against the original string, which you can get from the <attr>_before_type_cast method.

    Something like this should work:

    validates_each(attr_names) do |record, attr, val|
      if record.send("#{attr}_before_type_cast").to_f < 0.01
        record.errors.add(attr, "error message here")
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a javascript validation method so that, if more than
I am trying to write my own form validation script with jQuery. However, I
I'm trying to write a validation function that checks to see if an entry
I'm trying to write a custom validation class that validates that an email is
I'm trying to write a user name validation that has the following restrictions: Must
I'm trying to write a validation script that will validate XML against the NITF
I'm trying to write a validation to check that an Object instance can be
I am trying write a function that generates simulated data but if the simulated
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.