Fix `attributes_before_type_cast` for serialised attributes. #8078

Merged
merged 1 commit into from Oct 31, 2012

Conversation

Projects
None yet
6 participants
Contributor

nikitug commented Oct 30, 2012

Public method attributes_before_type_cast used to return internal AR structure (ActiveRecord::AttributeMethods::Serialization::Attribute), patch fixes this. Now behaves like read_attribute_before_type_cast and returns unserialised values.

Also it may need a backport to 3-2-stable.

Contributor

nikitug commented Oct 30, 2012

activerecord/lib/active_record/attribute_methods/serialization.rb
+
+ def attributes_before_type_cast
+ super.dup.tap do |attributes|
+ self.class.serialized_attributes.each do |key, _|
@carlosantoniodasilva

carlosantoniodasilva Oct 30, 2012

Owner

You can probably use each_key here and remove the _ arg.

@nikitug

nikitug Oct 31, 2012

Contributor

Right, my bad :)

activerecord/test/cases/serialized_attribute_test.rb
+ assert_equal({ :foo => :bar }, t.attributes_before_type_cast["content"])
+ t.save!
+ t.reload
+ assert_equal({ :foo => :bar }, t.attributes_before_type_cast["content"])
@carlosantoniodasilva

carlosantoniodasilva Oct 30, 2012

Owner

New hash styles, please.

@frodsan

frodsan Oct 30, 2012

Contributor

@carlosantoniodasilva Maybe not here because this is fixing an issue and it will be easier to backport.

It also needs a changelog entry, thanks!

Contributor

nikitug commented Oct 31, 2012

@carlosantoniodasilva thanks for the comments, done.
I've created a backport PR #8082 (with a cheangelog entry, so do we need it here?).
Also used an opportunity to convert hashes to 1.9 style 🎃

activerecord/CHANGELOG.md
@@ -979,4 +979,8 @@
*Aaron Patterson*
+* `AR::Base#attributes_before_type_cast` now returns unserialized values for serialized attributes.
@frodsan

frodsan Oct 31, 2012

Contributor

new CHANGELOG entries go to the top of the file.

Contributor

nikitug commented Oct 31, 2012

@frodsan thanks, fixed

Fix `attributes_before_type_cast` for serialised attributes.
Public method `attributes_before_type_cast` used to return internal AR structure (ActiveRecord::AttributeMethods::Serialization::Attribute), patch fixes this. Now behaves like `read_attribute_before_type_cast` and returns unserialised values.

carlosantoniodasilva added a commit that referenced this pull request Oct 31, 2012

Merge pull request #8078 from nikitug/serialized_attributes_before_ty…
…pe_cast

Fix `attributes_before_type_cast` for serialized attributes.

@carlosantoniodasilva carlosantoniodasilva merged commit 6569709 into rails:master Oct 31, 2012

nikitug added a commit to nikitug/rails that referenced this pull request Nov 9, 2012

Backport #8078: Fix `attributes_before_type_cast` for serialised attr…
…ibutes.

Public method attributes_before_type_cast used to return internal AR structure (ActiveRecord::AttributeMethods::Serialization::Attribute), patch fixes this. Now behaves like read_attribute_before_type_cast and returns unserialised values.

carlosantoniodasilva added a commit that referenced this pull request Nov 9, 2012

Merge pull request #8082 from nikitug/backport_serialized_attributes_…
…before_type_cast

Backport #8078: Fix `attributes_before_type_cast` for serialised attributes

Is this desired behavior? Now there's no way of accessing the serialized version of the data.

ttoomey commented Aug 8, 2013

work around to get serialized value

obj.instance_variable_get(:@attributes)['my_serialized_value'].serialized_value
Contributor

avit commented Oct 29, 2013

Doesn't this change do the wrong thing? I expect this should return the serialized value, (e.g. a JSON string) and not the unserialized / type cast value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment