Skip to content

Commit

Permalink
Fixed Time/Date object serialization
Browse files Browse the repository at this point in the history
Time/Date objects used to be converted to_s instead of to_uaml
which made them unserializable.
  • Loading branch information
tarmo authored and jeremy committed Aug 13, 2008
1 parent 1b127fc commit a5aad2e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2626,8 +2626,15 @@ def attributes_with_quotes(include_primary_key = true, include_readonly_attribut
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)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,12 @@ def test_serialized_attribute
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
Expand Down

0 comments on commit a5aad2e

Please sign in to comment.