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

#569: Don't create empty translations on read #668

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dorner
Copy link

@dorner dorner commented Jan 13, 2025

Fixes #569 .

This ended up being simpler than I expected. I simply added a for_read parameter that's only set on read, which would return an isolated translation object instead of one attached to the translations collection. This leaves the cache intact as well.

@dorner
Copy link
Author

dorner commented Jan 13, 2025

Hmm... might not be as simple. In this case if you first do a read, it'll cache the read as the isolated record. Then subsequent writes will go to the isolated record instead of the built one, and they won't save. Let me try to fix.

@dorner
Copy link
Author

dorner commented Jan 13, 2025

Should be fixed now!

Copy link
Owner

@shioyama shioyama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this work! I can see that this fixes the problem for your use case. The problem as I note above is that it also will create new instances on every read, which could be a problem.

I suggested another solution which, while not as easy to implement, is the right solution IMO. It might be too much to ask but if someone wants to ship this fix, that's the solution I would accept. However, since that would also change fundamentally how the backend works, it may have an impact on other things and need to be shipped at least in a minor release rather than a patch release.

<module fileurl="file://$PROJECT_DIR$/.idea/mobility.iml" filepath="$PROJECT_DIR$/.idea/mobility.iml" />
</modules>
</component>
</project>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this?

Comment on lines +311 to +313
if for_read
return translations.klass.new(locale: locale) if translation.nil?
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will repeatedly create instances on each read, which could be a problem in some hot spots.

I think a better approach would be to replace the build with an array of instances created with new (as you have above) rather than build, so that they are not saved by default.

Then we would need to add a save hook to assign the instances before saving. I believe this is how Globalize does it.

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.

Validations create empty translations in DB
2 participants