Rails + globalize - disabling translations autoloading

96 views Asked by At

I have an app with these gems:

rails 5.2.8.1

globalize 6.2.1

globalize-accessors 0.3.0

Seems translations are always autoloaded eventhough I'm not calling the accessors/translated columns. For example:

Model.where(id:123)
Model Load (3.0ms)  SELECT  "model"."id", "model"."column1", "model"."created_at", "model"."updated_at" FROM "model" WHERE "model"."id" = $1 LIMIT $2  [["id", 123], ["LIMIT", 11]]
Model::Translation Load (0.5ms)  SELECT "model_translations".* FROM "model_translations" WHERE "model_translations"."model_id" = $1  [["model_id", 123]]

why are translations auto loaded? How do I disable this? I also tried:

Model.where(id:123).select(:id)
Model Load (0.7ms)  SELECT  "model"."id" FROM "model" WHERE "model"."id" = $1 LIMIT $2  [["id", 123], ["LIMIT", 11]]
Model::Translation Load (0.4ms)  SELECT "model_translations".* FROM "model_translations" WHERE "model_translations"."model_id" = $1  [["model_id", 123]]

Is there a way to prevent calling SELECT on the translations table? Thanks

1

There are 1 answers

4
borisano On

Yes, there is a way to prevent the translations from being autoloaded. You can do this by setting the globalize_load_translations option to false in your model. For example:

class Model < ApplicationRecord
  globalize_load_translations false
end

This will prevent the translations from being loaded when you call Model.find(123). However, if you do need to access the translations, you can still do so by calling the translations method on the model instance. For example:

model = Model.find(123)
translations = model.translations

This will only load the translations if they are actually needed.

You can also disable the translation autoloading globally by setting the config.globalize.load_translations option to false in your Rails application configuration file. For example:

config.globalize.load_translations = false

This will disable the translation autoloading for all models in your application.