For example, say I have a User model with an integer column 'pet_id'.
If I run
user = User.new
user.update_attribute(:pet_id, '1')
It automatically converts the string '1' to an integer 1. Where does this conversion take place?
For example, say I have a User model with an integer column 'pet_id'.
If I run
user = User.new
user.update_attribute(:pet_id, '1')
It automatically converts the string '1' to an integer 1. Where does this conversion take place?
This is the method responsible for the type_cast in active record
def type_cast(value)
    return nil if value.nil?
    return coder.load(value) if encoded?
    klass = self.class
    case type
    when :string, :text        then value
    when :integer              then klass.value_to_integer(value)
    when :float                then value.to_f
    when :decimal              then klass.value_to_decimal(value)
    when :datetime, :timestamp then klass.string_to_time(value)
    when :time                 then klass.string_to_dummy_time(value)
    when :date                 then klass.value_to_date(value)
    when :binary               then klass.binary_to_string(value)
    when :boolean              then klass.value_to_boolean(value)
    else value
    end
  end
To understand rails activerecord type_cast in details please visit these three sites
1) Thoughtbot blog How Rails' Type Casting Works
2) Ken Collins ActiveRecord 4.2's Type Casting
3) Rails activerecord typecast method in github