From 9bf4583241e189d04a5df143332107ef18857034 Mon Sep 17 00:00:00 2001 From: Sen-Zhang Date: Mon, 14 Mar 2016 18:51:25 -0700 Subject: fix datatime error --- activerecord/CHANGELOG.md | 5 +++++ .../lib/active_record/attribute_methods/time_zone_conversion.rb | 2 +- activerecord/test/cases/multiparameter_attributes_test.rb | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 95cdc6bc3f..400f4942c4 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Fix an Active Record DateTime field NoMethodError caused by incomplete + datetime. [Bug #24195](https://github.com/rails/rails/issues/24195) + + *Sen Zhang* + * Allow `slice` to take an array of methods (without need for splatting). *Cohen Carlisle* diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb index ce9985e2e1..500d903857 100644 --- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb +++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb @@ -39,7 +39,7 @@ module ActiveRecord end def set_time_zone_without_conversion(value) - ::Time.zone.local_to_utc(value).in_time_zone if value + ::Time.zone.local_to_utc(value).try(:in_time_zone) if value end def map_avoiding_infinite_recursion(value) diff --git a/activerecord/test/cases/multiparameter_attributes_test.rb b/activerecord/test/cases/multiparameter_attributes_test.rb index 18ecc67f0b..ceb5724377 100644 --- a/activerecord/test/cases/multiparameter_attributes_test.rb +++ b/activerecord/test/cases/multiparameter_attributes_test.rb @@ -260,6 +260,13 @@ class MultiParameterAttributeTest < ActiveRecord::TestCase topic.attributes = attributes assert_equal Time.zone.local(2000, 1, 1, 16, 24, 0), topic.bonus_time assert_not topic.bonus_time.utc? + + attributes = { + "written_on(1i)" => "2000", "written_on(2i)" => "", "written_on(3i)" => "", + "written_on(4i)" => "", "written_on(5i)" => "" + } + topic.attributes = attributes + assert_nil topic.written_on end ensure Topic.reset_column_information -- cgit v1.2.3