diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 7eff9e9c939b822cc079fd90c234287f299e4456..cda2262ed4897a53c383edd4e7cd2c799a7e5ab4 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Use Date#to_s(:db) for quoted dates. #7411 [Michael Schoen] + * Don't create instance writer methods for class attributes. Closes #7401 [Rick] * Docs: validations examples. #7343 [zackchandler] diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index ce9495a5d23027fa36064ca68fef0971c84765d6..aa405eb47c8b3d0765d17e4e8cded90fe4896e32 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -25,9 +25,7 @@ def quote(value, column = nil) # BigDecimals need to be output in a non-normalized form and quoted. when BigDecimal then value.to_s('F') else - if value.acts_like?(:date) - "'#{value.to_s}'" - elsif value.acts_like?(:time) + if value.acts_like?(:date) || value.acts_like?(:time) "'#{quoted_date(value)}'" else "'#{quote_string(value.to_yaml)}'" @@ -50,13 +48,13 @@ def quote_column_name(name) def quoted_true "'t'" end - + def quoted_false "'f'" end - + def quoted_date(value) - value.strftime("%Y-%m-%d %H:%M:%S") + value.to_s(:db) end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index eb9d0e94dfdd5f92d58035a9da00134e9356b4fe..2f4b2b9a3a63963698cd7dd070d30867202cb67b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -125,8 +125,13 @@ def quote_column_name(name) %("#{name}") end + # Include microseconds if the value is a Time responding to usec. def quoted_date(value) - value.strftime("%Y-%m-%d %H:%M:%S.#{sprintf("%06d", value.usec)}") + if value.acts_like?(:time) && value.respond_to?(:usec) + "#{super}.#{sprintf("%06d", value.usec)}" + else + super + end end