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

  • Home
  • SEARCH
  • 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 5930237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:31:44+00:00 2026-05-22T14:31:44+00:00

I have the following rails model: class Product < ActiveRecord::Base end class CreateProducts <

  • 0

I have the following rails model:

class Product < ActiveRecord::Base
end

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.decimal :price

      t.timestamps
    end
  end

  def self.down
    drop_table :products
  end
end

But when I do the following in the rails console:

ruby-1.9.2-p180 :001 > product = Product.new
 => #<Product id: nil, price: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :002 > product.price = 'a'
 => "a" 
ruby-1.9.2-p180 :003 > product.save
 => true 
ruby-1.9.2-p180 :004 > p product
#<Product id: 2, price: #<BigDecimal:39959f0,'0.0',9(9)>, created_at: "2011-05-18 02:48:10", updated_at: "2011-05-18 02:48:10">
 => #<Product id: 2, price: #<BigDecimal:3994ca8,'0.0',9(9)>, created_at: "2011-05-18 02:48:10", updated_at: "2011-05-18 02:48:10"> 

As you can see, I wrote ‘a’ and it saved 0.0 in the database. Why is that? This is particularly annoying because it bypasses my validations e.g.:

class Product < ActiveRecord::Base
  validates :price, :format => /\d\.\d/
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-22T14:31:45+00:00Added an answer on May 22, 2026 at 2:31 pm

    anything that is invalid gets cast to 0.0 if you call to_f on it

    "a".to_f #=> 0.0

    you would need to check it with validations in the model

    validates_numericality_of :price # at least in rails 2 i think

    i dont know what validating by format does, so i cant help you there, but try to validate that it is a number, RegExs are only checked against strings, so if the database is a number field it might be messing up

    :format is for stuff like email addresses, logins, names, etc to check for illegeal characters and such

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

Sidebar

Related Questions

I have the following Rails model: class CreateFoo < ActiveRecord::Migration def self.up create_table :foo
I have a ActiveRecord Car model: class Car < ActiveRecord::Base def check_value puts self.load_size
I have the following model, Purchase, on my Rails app: class Purchase < ActiveRecord::Base
I have the following model: class Advisor < ActiveRecord::Base belongs_to :course end class Course
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I have a simple model: class User < ActiveRecord::Base has_and_belongs_to_many :roles end class Role
I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have a model class as shown below: class Product < ActiveRecord::Base belongs_to :user;
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have the following model and methods: class UserPrice < ActiveRecord::Base attr_accessible :price, :purchase_date,

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.