Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix for time type columns with invalid time value #7337

Merged
merged 2 commits into from

3 participants

@adzap

The string_to_dummy_time method was blindly parsing the dummy time string
with Date._parse which returns a hash for the date part regardless
of whether the time part is an invalid time string.

@danbeaulieu

+1. Is there a work around in the meantime?

@rafaelfranca
Owner

What is the current behavior? What is the expected behavior? And why? Please send more information about the issue, it will make easier to review and merge.

@danbeaulieu

@rafaelfranca,
I have an open question on SO about this: http://stackoverflow.com/questions/12263135/time-type-field-getting-bad-default-datetime-if-it-is-invalid

Current behavior is if you have a :time type attribute on a model and you set it to some invalid time, like 'foo', ActiveRecord happily parses it into a valid datetime object.

Expected behavior would be similar to :date and :datetime where if the input is invalid, nil is the result.

Essentially what happens with :time is it comes down to Date._parse("2000-01-01 foo") which will parse into a hash with some values, but since this is specifically a :time type, it doesn't validate the the hash has at least a value for :hour.

Hope this helps.

@rafaelfranca
Owner

Make sense. @adzap lets move this forward to merge. Could you add a CHANGELOG entry?

@adzap

@rafaelfranca will do.

@danbeaulieu thanks for putting in the explanation.

adzap added some commits
@adzap adzap Fix for time type columns with invalid time
The string_to_dummy_time method was blindly parsing the dummy time string
with Date._parse which returns a hash for the date part regardless
of whether the time part is an invalid time string.
ce7cdb9
@adzap adzap Update changelog with time column type casting fix acf583a
@adzap

@rafaelfranca Changelog updated. This change can/should be back ported to v3 branches too.

@rafaelfranca rafaelfranca merged commit 91183ba into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 5, 2012
  1. @adzap

    Fix for time type columns with invalid time

    adzap authored
    The string_to_dummy_time method was blindly parsing the dummy time string
    with Date._parse which returns a hash for the date part regardless
    of whether the time part is an invalid time string.
  2. @adzap
This page is out of date. Refresh to see the latest.
View
4 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*
View
8 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
View
12 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
Something went wrong with that request. Please try again.