Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialized attributed with native hash is saved as !ruby/hash:ActiveSupport::HashWithIndifferentAccess #391

Closed
radeno opened this issue Jul 7, 2014 · 5 comments
Milestone

Comments

@radeno
Copy link

radeno commented Jul 7, 2014

Hi,

when i save native hash to database as serialized attribute, paper_trail it's saves as hash with indifferent access. Is there any switch to turn this behavior off?

For example:

SQL result after save my object

---\n:en: TEXT IN ENGLISH\n:de: TEXT IN DEUTSCH\n

and paper_trail version of that objet

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n  en: TEXT IN ENGLISH\n  de: TEXT IN DEUTSCH\n

in my model i symbolize_keys with before_validation action.

serialize :attribute_translations
before_validation :normalize_translations

def normalize_translations
  self.attribute_translations = self.attribute_translations.symbolize_keys
end
@batter
Copy link
Collaborator

batter commented Jul 7, 2014

Sorry, I'm not entirely sure what the question is. Are you saying it is saving the attribute as a HashWithIndifferentAccess in you versions table? PaperTrail typically serializes data into YAML or JSON (depending on how you set it up) before inserting into the database unless you are using Postgres and have the native JSON column type available.

@NullVoxPopuli
Copy link
Contributor

I noticed this the other day, too.
It seems like if a change_attribute is a native rails serialized field, it stores ruby/hash:TypeOfHash.

Maybe does teh JSON serializer serialize recursively? I wonder if it's running in to an issue where one of the nested types doesn't respond to to/as_json?

@batter
Copy link
Collaborator

batter commented Sep 24, 2014

I think this is the default behavior of YAML in rails. You can work around this by switching your default serializer to JSON. See instructions on README for how to do this.

If you have legacy data that is already serialized in the YAML format, then you can take a look at this comment on #388 for some insight as to how to convert that data to JSON format.

@batter
Copy link
Collaborator

batter commented Oct 21, 2014

Ok, did some more investigation into this. It seems this only happens in the object_changes hash, and it is because when we start composing the value from that column, we initially infer the value from model_instance.changes, as can be seen in the code here, which is returned as an ActiveSupport::HashWithIndifferentAccess. So we can actually cut down on the amount of YAML being stored by the gem when YAML is the serializer by casting this value to a Hash... I'll see if I can get something merged soon that will tackle this.

@batter batter added this to the 3.1.0 milestone Oct 21, 2014
@batter batter closed this as completed in 08d89bd Oct 21, 2014
@radeno
Copy link
Author

radeno commented Dec 2, 2014

it looks really great! thank you so much.
HashWithIndifferentAccess is a mess and i hope one time will be removed :)

Thank you again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants