This project is archived and is in readonly mode.

#2881 ✓stale
Peter Abrahamsen

Can't define accessors for serialized attributes

Reported by Peter Abrahamsen | July 8th, 2009 @ 03:58 AM | in 3.0.2

Hi there. With ordinary attributes, I can write an accessor like this:

class Foo < ActiveRecord::Base
  # column :foo, :integer
  def foo
    read_attribute(:foo) || write_attribute(:foo, 123)

and I get, effectively, a default value for the column. There are lots of other uses for writing your own attribute accessors.

This doesn't work with serialized attributes:

class Foo < ActiveRecord::Base
  # column :bar, :text
  serialize :bar, Hash

  def bar
    read_attribute(:bar) || write_attribute(:bar,

f = # nil

AR is overriding my accessor with its own. Consequently, everywhere I want to use the "bar" attribute, I have to say something like "( ||=[:a] = 1".

Comments and changes to this ticket

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=""></a>