Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Can't define accessors for serialized attributes

#2881

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)
  end
end

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, Hash.new)
  end
end

f = Foo.new
f.bar # nil

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

Reported by Peter Abrahamsen · July 8th, 2009 @ 03:58 AM

State: stale
Milestone: 3.0.2
Assigned to: josh josh
Importance: none

Activity

  1. CancelProfileIsBroken
  2. Michael Koziarski
    Michael Koziarski
    • Assigned user set to josh
    • Milestone cleared.

    Serialized accessors appear to be unconditionally defined.

    August 8th, 2009 @ 03:16 AM

  3. Michael Koziarski
  4. josh
    josh
    • State changed from new to open
    • Tag set to bugmash

    Can you please attach a failing unit test.

    August 10th, 2009 @ 02:14 AM

  5. josh
    josh
    • State changed from open to incomplete

    August 19th, 2009 @ 03:58 PM

  6. josh
    josh
    • State changed from incomplete to stale

    August 31st, 2009 @ 06:57 PM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM