-
Notifications
You must be signed in to change notification settings - Fork 82
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
Globalize migration guide: eager loading and language switching? #390
Comments
Howdy! 👋 Yeah documentation is sparse on this topic. Globalize has I just looked up what Globalize does and here's what it looks like: preload(:translations).joins(:translations).readonly(false).with_locales(locales).tap do |query|
query.distinct! unless locales.flatten.one?
end You can get the same effect with Mobility but there is no def with_locales(*locales)
# Avoid using "IN" with SQL queries when only using one locale.
locales = locales.flatten.map(&:to_s)
locales = locales.first if locales.one?
where :locale => locales
end So basically it's adding a Honestly, this is too much indirection for me and one of the reasons I think Globalize ends up with lots of issues. Mobility already has too much magic (trying to improve that) so definitely don't want to add anything. Also, as mentioned, this is very specific to the strategy, so wouldn't generalize to other backends. That said if there was a rough equivalent, it would make sense to document it in the wiki. I just made the wiki editable to anyone, would you like to add something about this to the migration guide? We can discuss here first if you like, and once a rough mapping is working, add that to the wiki. |
At this point I was not looking for a general solution for all backends but only making my Globalize => Mobility + After this will indeed come the question of how to refactor the backend from I will check if anything can be added to the wiki once I'm done with my PR. |
See also #105 |
@shioyama would you mind expanding on why this is not recommended? |
@shioyama so for my specific model (which has @posts = Post
- .with_translations(I18n.locale)
+ .eager_load(:translations)
+ .where.not(title: nil) Is this the "official"/"recommended" approach to load all posts available in a specific language? When reading that code, I just feel that the intent is not as clear, and I feel inclined to add this to my model: scope :with_translations, ->{ eager_load(:translations).where.not(title: nil) } Then I can just call |
So I just started using Mobility in production, here is what I did: First to make thing easier and avoid having a huge PR (e.g with extra # app/models/concerns/translatable.rb
module Translatable
extend ActiveSupport::Concern
included do
extend Mobility
default_scope { i18n }
scope :with_locales, -> do
where.not(self.translation_detection_column => nil)
end
scope :with_translations, -> do
eager_load(:translations).with_locales
end
def self.translation_detection_column
in?([Testimonial, Thought]) ? :quote : :title
end
end
end (NB: the code impact could be made even smaller by allowing Second, I made these various changes:
Third, I had various models like this: class Post < ApplicationRecord
...
translates :title, :slug
has_paper_trail
class Translation
validates :title, presence: true
has_paper_trail
def slug=(slug)
self[:slug] = slug || title&.parameterize
end
end
end So for this example I did the following changes:
Here is the result: class Post < ApplicationRecord
include Translatable
...
translates :title
validates :title, presence: true
def slug=(slug)
super(slug || title&.parameterize)
end
end More generally I tried to remove the various references to Now it all works! 🎉 (except for Papertrail) |
I am migrating from Globalize following the migration guide, however it appears to be missing instructions on eager loading as well as checking for the existence of a translation.
This is how I understand I should rewrite eager loading to avoid N+1 queries:
But how about replacing this, which I had been using in a language switcher?
NB (a bit off-topic): maybe to check for the presence of the translation, I just need to check if
I18n.with_locale(:other_locale) { @post.title }
is truthy? Is there any method to check for the presence of the translation in a general way that works for all models (without assuming the presence of atitle
)?Context
Migrating from Globalize 5.3.0 to Mobility 0.0.13.
Expected Behavior
More detailed instructions in the wiki guide about:
Actual Behavior
Missing instructions.
Possible Fix
Add info to Globalize migration guide.
The text was updated successfully, but these errors were encountered: