Skip to content
  • Marcelo Lauxen's avatar
    5fdc7d38
    Fix `read_attribute_before_type_cast` to consider attribute aliases · 5fdc7d38
    Marcelo Lauxen authored
    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.
    5fdc7d38
    Fix `read_attribute_before_type_cast` to consider attribute aliases
    Marcelo Lauxen authored
    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.
Loading