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

Fix missing translated attributes in as_json #630

Closed

Conversation

scarfacedeb
Copy link

@scarfacedeb scarfacedeb commented Nov 7, 2023

This PR fixes a regression that I noticed in ActiveRecord::Base#as_json return value.

Description

If you enable attribute_methods plugin, mobility patches ActiveRecord::Base#attributes method to include translated attributes as wekk. The same change used to apply to ActiveRecord::Base#as_json too.

But the internal implementation of serialization changed slightly in Rails 7.0 to improve the performance.

As a result, as_json method no longer includes translated attributes.

Solution

I added an override of attribute_names_for_serialization method into the plugin to make it include the missing translated fields into serialized_attributes hash which is used by as_json later.

attribute_names_for_serialization would be never be called by Rails 6.x or lower, but I still added a safe guard against calling super.

@shioyama
Copy link
Owner

Thanks! Can you add an entry to the changelog and rebase this?

@ollym ollym mentioned this pull request Jul 23, 2024
@shioyama
Copy link
Owner

shioyama commented Dec 3, 2024

Shipped in #660

@shioyama shioyama closed this Dec 3, 2024
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

Successfully merging this pull request may close these issues.

2 participants