This project is archived and is in readonly mode.

#194 ✓resolved
Jacob Atzen

Serialized timestamps returns strings on load

Reported by Jacob Atzen | May 14th, 2008 @ 03:51 PM | in 2.1.1

When serializing a Time object into a textfield by the means of ActiveRecord::Base.serialize what comes back is a string.

Example:

c = CacheItem.create(:key => "time", :value => Time.now)

c.value => "2008-05-14 16:45:03"

Comments and changes to this ticket

  • Rick

    Rick May 14th, 2008 @ 03:52 PM

    • State changed from “new” to “incomplete”

    What does your model look like (specifically, the serialize call)?

    What does your model table schema look like (specifically the value field)?

    Why don't use use a datetime column type?

  • Jacob Atzen

    Jacob Atzen May 14th, 2008 @ 04:10 PM

    My schema definition is:

    create_table :cached_method_items do |t|

    t.string :key

    t.text :value

    t.datetime :expires_at

    t.timestamps

    end

    The model is hidden away in a module (I'm trying to create a plugin):

    module CachedMethod #:nodoc:

    class Item < ActiveRecord::Base

    set_table_name "cached_method_items"

    serialize :value

    end

    end

    The reason I'm not using a datetime is that this is a cache which should be

    able to cache any kind of object. Serializing hashes works fine, serializing

    a hash with a timestamp works fine, just not a timestamp object on it's own.

    If I look in the database I can see the timestamp is serialized as

    "2008-05-14 16:59:45" - whereas if I call YAML::dump(Time.now) I get "---

    2008-05-14 17:02:36.626834 +02:00\n". I'm using sqlite3 if that's of any

    relevance.

  • Tarmo Tänav

    Tarmo Tänav August 13th, 2008 @ 02:01 AM

    • Tag set to activerecord, bug

    I can confirm this problem, it's clear from http://github.com/rails/rails/tr... that every time/date value gets converted to_s instead of to_yaml (which is required for serialized attributes), and since the string the time/date value is converted to is not unserializable it is treated as just a string.

    I'm not sure what would be a good fix here because clearly the quoting logic is very much divorced from any knowledge about serialization so it can't really decide on how to properly serialize time/date values for serialized columns.

  • Tarmo Tänav
  • Jeremy Kemper

    Jeremy Kemper August 13th, 2008 @ 05:04 AM

    • State changed from “incomplete” to “resolved”
    • Milestone set to 2.1.1

    dc5997f

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages