Home > Mobile >  Rails ignores the default date format after upgrading from 6.1 to 7.0
Rails ignores the default date format after upgrading from 6.1 to 7.0

Time:02-19

Our application previously defined the default date format as DD/MM/YYYY in config/application.rb like so:

Date::DATE_FORMATS[:default] = '%d/%m/%Y'

This worked as expected in Rails 6.1, but after upgrading to Rails 7.0 it now appears to be ignored by .to_s:

Loading development environment (Rails 7.0.2.2)
3.0.1 :001 > Date::DATE_FORMATS[:default]
 => "%d/%m/%Y" 
3.0.1 :002 > Date.new(2022, 12, 31).to_s
 => "2022-12-31"
3.0.1 :003 > Date.new(2022, 12, 31).to_fs
 => "31/12/2022" 

How can I have .to_s implement this behaviour in Rails 7.0 ?

CodePudding user response:

This feature was deprecated in Rails 7. You can turn it back on with config.active_support.disable_to_s_conversion

Deprecate passing a format to #to_s in favor of #to_fs in Array, Range, Date, DateTime, Time, BigDecimal, Float and, Integer.

This deprecation is to allow Rails application to take advantage of a Ruby 3.1 optimization that makes interpolation of some types of objects faster.

New applications will not have the #to_s method overridden on those classes, existing applications can use config.active_support.disable_to_s_conversion.

  • Related