Method: ActiveRecord::ConnectionAdapters::MySQL::Quoting#type_cast

Defined in:
activerecord/lib/active_record/connection_adapters/mysql/quoting.rb

#type_cast(value) ⇒ Object

Override type_cast we pass to mysql2 Date and Time objects instead of Strings since MySQL adapters are able to handle those classes more efficiently.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'activerecord/lib/active_record/connection_adapters/mysql/quoting.rb', line 94

def type_cast(value) # :nodoc:
  case value
  when ActiveSupport::TimeWithZone
    # We need to check explicitly for ActiveSupport::TimeWithZone because
    # we need to transform it to Time objects but we don't want to
    # transform Time objects to themselves.
    if default_timezone == :utc
      value.getutc
    else
      value.getlocal
    end
  when Time
    if default_timezone == :utc
      value.utc? ? value : value.getutc
    else
      value.utc? ? value.getlocal : value
    end
  when Date
    value
  else
    super
  end
end