Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AR 4.2 type_casting decimal without precision #18122

Closed
bradleypriest opened this issue Dec 21, 2014 · 1 comment
Closed

AR 4.2 type_casting decimal without precision #18122

bradleypriest opened this issue Dec 21, 2014 · 1 comment
Milestone

Comments

@bradleypriest
Copy link
Contributor

Upgrading from 4.1.8 to 4.2.0 I have run into a weird edge-case for PG decimal columns without precisions defined.
It was a bug in my application code that we didn't have the precision defined.
However, if Postgres supports it, so we probably should try to too.

gem 'activerecord', '4.2.0'
require 'active_record'

# # Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"

# Connect to an in-memory sqlite3 database (more on this in a moment)
ActiveRecord::Base.establish_connection(
  :adapter  => 'sqlite3',
  :database => ':memory:'
)

# Create the minimal database schema necessary to reproduce the bug
ActiveRecord::Schema.define do
  create_table :users, :force => true do |t|
    t.decimal :allowance
  end
end

# Create the minimal set of models to reproduce the bug
class User < ActiveRecord::Base
end

User.new(allowance: 22.68).allowance == 22.68.to_d # fails

This seems to be due to the cast_value method in this file. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/type/decimal.rb#L19

In this case because the precision.to_i is zero, the float_precision is zero and we end up calling BigDecimal.new(22.68, 0) which returns #<BigDecimal:7fd859c87cb8,'0.2267999999 9999999715 7829056959 59925652E2',45(54)>

This functionality seems to have been changed in #16333

@senny senny added this to the 4.2.1 milestone Dec 22, 2014
@senny
Copy link
Member

senny commented Dec 22, 2014

/cc @sgrif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants