This project is archived and is in readonly mode.
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)
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".
Comments and changes to this ticket
-
CancelProfileIsBroken August 7th, 2009 @ 02:26 PM
- Tag set to bugmash
-
Michael Koziarski August 8th, 2009 @ 03:16 AM
- Assigned user set to josh
- Milestone cleared.
Serialized accessors appear to be unconditionally defined.
-
Michael Koziarski August 8th, 2009 @ 03:17 AM
- Tag cleared.
-
josh August 10th, 2009 @ 02:14 AM
- State changed from new to open
- Tag set to bugmash
Can you please attach a failing unit test.
-
josh August 19th, 2009 @ 03:58 PM
- State changed from open to incomplete
-
josh August 31st, 2009 @ 06:57 PM
- State changed from incomplete to stale
-
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to
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>