diff options
author | Tarmo Tänav <tarmo@itech.ee> | 2008-08-13 06:18:01 +0300 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-12 20:29:07 -0700 |
commit | a5aad2e81febfa1a8d9fea0faffb5a3b4535982b (patch) | |
tree | f1936fe0655d0dd5fd0a6da1ac1e1d5d5b7d61e2 | |
parent | 1b127fcdea28d6c6ab2f2c6370125c8817ba99d6 (diff) | |
download | rails-a5aad2e81febfa1a8d9fea0faffb5a3b4535982b.tar.gz rails-a5aad2e81febfa1a8d9fea0faffb5a3b4535982b.tar.bz2 rails-a5aad2e81febfa1a8d9fea0faffb5a3b4535982b.zip |
Fixed Time/Date object serialization
Time/Date objects used to be converted to_s instead of to_uaml
which made them unserializable.
-rw-r--r-- | activerecord/lib/active_record/base.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 6 |
2 files changed, 15 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index e5b6e3a02f..2c4ead081d 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2626,8 +2626,15 @@ module ActiveRecord #:nodoc: quoted = {} connection = self.class.connection attribute_names.each do |name| - if column = column_for_attribute(name) - quoted[name] = connection.quote(read_attribute(name), column) unless !include_primary_key && column.primary + if (column = column_for_attribute(name)) && (include_primary_key || !column.primary) + value = read_attribute(name) + + # We need explicit to_yaml because quote() does not properly convert Time/Date fields to YAML. + if value && self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time)) + value = value.to_yaml + end + + quoted[name] = connection.quote(value, column) end end include_readonly_attributes ? quoted : remove_readonly_attributes(quoted) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 0f9eda4d09..36d30ade5e 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1361,6 +1361,12 @@ class BasicsTest < ActiveRecord::TestCase assert_equal(myobj, topic.content) end + def test_serialized_time_attribute + myobj = Time.local(2008,1,1,1,0) + topic = Topic.create("content" => myobj).reload + assert_equal(myobj, topic.content) + end + def test_nil_serialized_attribute_with_class_constraint myobj = MyObject.new('value1', 'value2') topic = Topic.new |