mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
5fdc7d385f
Numericality validations for aliased attributes are not able to get the value of the attribute before type cast because activerecord was trying to get the value of the attribute based on attribute alias name and not the original attribute name. Example of validation which would pass even if a invalid value would be provided class MyModel < ActiveRecord::Base validates :aliased_balance, numericality: { greater_than_or_equal_to: 0 } end If we instantiate MyModel like bellow it will be valid because when numericality validation runs it will not be able to get the value before type cast, so it uses the type casted value which will be `0.0` and the validation will match. subject = MyModel.new(aliased_balance: "abcd") subject.valid? But if we declare MyModel like this class MyModel < ActiveRecord::Base validates :balance, numericality: { greater_than_or_equal_to: 0 } end and assign "abcd" value to `balance` when the validations run the model will be invalid because activerecord will be able to get the value before type cast. With this change `read_attribute_before_type_cast` will be able to get the value before type cast even when the attr_name is an attribute_alias.
12 lines
439 B
Ruby
12 lines
439 B
Ruby
# frozen_string_literal: true
|
|
|
|
class NumericData < ActiveRecord::Base
|
|
self.table_name = "numeric_data"
|
|
# Decimal columns with 0 scale being automatically treated as integers
|
|
# is deprecated, and will be removed in a future version of Rails.
|
|
attribute :world_population, :big_integer
|
|
attribute :my_house_population, :big_integer
|
|
attribute :atoms_in_universe, :big_integer
|
|
|
|
alias_attribute :new_bank_balance, :bank_balance
|
|
end
|