Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

avoid nil.dup

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
commit 40aefb93018277ee7cafc5529b16d7b6df8aa4dd 1 parent 1fd9d97
@amatsuda amatsuda authored spastorino committed
View
2  activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -41,7 +41,7 @@ def define_method_attribute=(attr_name)
if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
method_body, line = <<-EOV, __LINE__ + 1
def #{attr_name}=(original_time)
- time = original_time.dup
+ time = original_time.dup unless original_time.nil?
@adzap
adzap added a note

You also can't dup a Fixnum.

@spastorino Owner

Please provide a patch using respond_to?

Won't work, dup is defined via Kernel module for every object.

@spastorino Owner

@brianopia yes you're right

@tenderlove Owner

Do people pass Fixnums to this method?

@adzap
adzap added a note

Apparently you can use fixnum in seconds as a value for the Mysql duration data type.

Is there a duration data type in Mysql?

@tenderlove Owner

@brianmario I don't see one. @adzap, can you show us?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
unless time.acts_like?(:time)
time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
end
View
8 activerecord/test/cases/attribute_methods_test.rb
@@ -460,6 +460,14 @@ def test_accessing_cached_attributes_caches_the_converted_values_and_nothing_els
end
end
+ def test_write_nil_to_time_attributes
+ in_time_zone "Pacific Time (US & Canada)" do
+ record = @target.new
+ record.written_on = nil
+ assert_nil record.written_on
+ end
+ end
+
def test_time_attributes_are_retrieved_in_current_time_zone
in_time_zone "Pacific Time (US & Canada)" do
utc_time = Time.utc(2008, 1, 1)

2 comments on commit 40aefb9

@brainopia

Won't work, dup is defined via Kernel module for every object.

@adzap

Apparently you can use fixnum in seconds as a value for the Mysql duration data type.

@adzap

@tenderlove @brianmario upon searching, I can't find one. I was told about it in a bug report elsewhere. Perhaps they used 'data type' too liberally. I am not up to date with Mysql features so I didn't question it.

Perhaps we can ignore that case.

@francocatena

This error also affects 3-0-stable, I create a patch in lighthouse for the same problem: https://rails.lighthouseapp.com/projects/8994/tickets/6367-timezone-aware-attributes-fail-with-nil-assignments

Please sign in to comment.
Something went wrong with that request. Please try again.