diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 85880e97eae1d9c1558f3f1ecd5a06cfbc2bbfaf..68c43558fddca50844a54dcf00c73a0ed9d75f5f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Fix time column type casting for invalid time string values to correctly return nil. + + *Adam Meehan* + * Allow to pass Symbol or Proc into :limit option of #accepts_nested_attributes_for *Mikhail Dieterle* diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 1445bb3b2f9388c3172bdca6f5a8f3474489c511..d0237848c7337356062cca364538713f55403311 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -178,7 +178,13 @@ def string_to_dummy_time(string) return string unless string.is_a?(String) return nil if string.blank? - string_to_time "2000-01-01 #{string}" + dummy_time_string = "2000-01-01 #{string}" + + fast_string_to_time(dummy_time_string) || begin + time_hash = Date._parse(dummy_time_string) + return nil if time_hash[:hour].nil? + new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) + end end # convert something to a boolean diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 9fcee99222bdb51ca6b2eabda24950ff1745cb2b..b9d480d9ce899fea1390342d8e2b4b87416f4946 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -970,6 +970,18 @@ def test_attributes_on_dummy_time assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time end + def test_attributes_on_dummy_time_with_invalid_time + # Oracle, and Sybase do not have a TIME datatype. + return true if current_adapter?(:OracleAdapter, :SybaseAdapter) + + attributes = { + "bonus_time" => "not a time" + } + topic = Topic.find(1) + topic.attributes = attributes + assert_nil topic.bonus_time + end + def test_boolean b_nil = Boolean.create({ "value" => nil }) nil_id = b_nil.id