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 952767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:56:50+00:00 2026-05-15T23:56:50+00:00

consider that i have a migration as follows create_table :dummies do |t| t.decimal :the_dummy_number

  • 0

consider that i have a migration as follows

create_table :dummies do |t|
  t.decimal :the_dummy_number
end 

i instantiate like the following

dummy = Dummy.new
dummy.the_dummy_number = "a string"
puts dummy.the_dummy_number

the output for the above is

 0.0

how did this happen? since i assign a wrong value shouldn’t it raise an error?

The biggest problem is the following.

Since it automatically converts my validate method fails miserably.

update-the validate method

 validate :is_dummy_number_valid, :the_dummy_number
 def is_dummy_number_valid
    read_attribute(:the_dummy_number).strip()
 end
  • 1 1 Answer
  • 1 View
  • 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-15T23:56:51+00:00Added an answer on May 15, 2026 at 11:56 pm

    The reason that this does not work as you expect is that the underlying ruby implementation of BigDecimal does not error when passed a string.

    Consider the following code

    [ 'This is a string', '2is a string', '2.3 is also a string', 
      '   -3.3 is also a string'].each { |d| puts "#{d} = #{BigDecimal.new(d)}" }
    
    This is a string = 0.0
    2is a string = 2.0
    2.3 is also a string = 2.3
       -3.3 is also a string = -3.3
    

    So BigDecimal scans the string and assigns anything at the beginning of the string that could be a decimal to its value.

    If you set your model up like this

    class Dummy < ActiveRecord::Base
    
       validates_numericality_of :the_dummy_number
    
    end
    

    Then the validation should work fine

    >> d=Dummy.new(:the_dummy_number => 'This is a string')
    => #<Dummy id: nil, the_dummy_number: #<BigDecimal:5b9230,'0.0',4(4)>, created_at: nil, updated_at: nil>
    
    >> puts d.the_dummy_number
    0.0
    => nil
    >> d.valid?
    => false
    
    >> d.errors
    => #<ActiveRecord::Errors:0x5af6b8 @errors=#<OrderedHash
      {"the_dummy_number"=>[#<ActiveRecord::Error:0x5ae114 
       @message=:not_a_number, @options={:value=>"This is a string"}
    

    This works because the validates_numericality_of macro uses the raw_value method to get at the value before it was typecast and assigned to the internal decimal value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets consider that I have a public property called AvatarSize like the following, public
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider that I have an interface that contains the following property interface IFoo {
Consider that i have two entities with following relationship: Entity A <-->> Entity B
Consider that I have the following javascript that do a post: $.post("/MyController/SomeAction", { myParam:
Consider that I have a MainWindow that has the following button declared in its
Consider that I have a main Thread which executes a new Runnable in a
Consider that I have a transaction: BEGIN TRANSACTION DECLARE MONEY @amount SELECT Amount AS
Assume that we have multiple arrays of integers. You can consider each array as
I have a configuration file that I consider to be my base configuration. I'd

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.