You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, Im setting up Mobility in our Rails app. It works when working in the console setting and saving data. But a problem appeared when we ran all our test suite. Consider a model called "General", we have a test helper that helps us to create one instance of this model with:
This helper fails in the update! method execution. I was debugging activerecord and ocurrs specifically in the save! internal execution of Rails. This is the full error:
PG::NotNullViolation: ERROR: null value in column "valor" of relation "generals" violates not-null constraint
DETAIL: Failing row contains (992314608, 2024-10-15 18:18:44.388633, doble_trabajo, 2024-10-15 18:18:44.388633, null).
I tried different configurations of Mobility and in the model but nothing works :( These are the files currently:
General model
# frozen_string_literal: true# == Schema Information## Table name: generals## created_at :datetime not null# id :integer not null, primary key# nombre :string not null# updated_at :datetime not null# valor :string not null## Indexes## general_unique_name (nombre) UNIQUE#classGeneral < ApplicationRecord# rubocop:disable Metrics/ClassLengthextendMobilitytranslates:valor,type: :string# MODOS DE MANTENIMIENTOMAINTENANCE_MODES=['starter_enterprise'].freezeCACHE_KEY="General.generales_hash"has_paper_trailmeta: nilvalidates:nombre,uniqueness: truevalidates:valor,presence: true,if: proc{valor.nil?}# permitimos string vacio pero no nilvalidate:validate_activar_agrupacion_items_descripcionvalidate:validar_traduccion_employee_no_vaciavalidate:validar_traduccion_employee_sin_espaciosvalidate:validar_traduccion_recintos_no_vaciavalidate:validar_traduccion_empresa_no_vaciavalidate:validar_traduccion_empresa_sin_espaciosvalidate:validar_traduccion_empresa_limite_veinticinco_caracteresvalidate:validar_activar_autogeneracion_codigo_de_ficha_importadoresvalidate:validar_empresa_nomina_transfer_agrupada_por_bancobefore_save:set_permissions#TODO: Eliminar en el featurebefore_save:set_value_agrupacionbefore_save:set_value_autogeneracion_codigo_de_ficha_importadoresbefore_save:deactivate_normal_user_amonestacionesbefore_save:activate_sso_config_on_asistenciaafter_commit{General.recargar}
...
Mobility Config
Mobility.configuredo# PLUGINSpluginsdo# Backend## Sets the default backend to use in models. This can be overridden in models# by passing +backend: ...+ to +translates+.## To default to a different backend globally, replace +:key_value+ by another# backend name.#backend:key_value# ActiveRecord## Defines ActiveRecord as ORM, and enables ActiveRecord-specific plugins.active_record# Accessors## Define reader and writer methods for translated attributes. Remove either# to disable globally, or pass +reader: false+ or +writer: false+ to# +translates+ in any translated model.#readerwriter# Backend Reader## Defines reader to access the backend for any attribute, of the form# +<attribute>_backend+.#backend_reader## Or pass an interpolation string to define a different pattern:# backend_reader "%s_translations"# Query## Defines a scope on the model class which allows querying on# translated attributes. The default scope is named +i18n+, pass a different# name as default to change the global default, or to +translates+ in any# model to change it for that model alone.#query# Cache## Comment out to disable caching reads and writes.#cache# Dirty## Uncomment this line to include and enable globally:# dirty## Or uncomment this line to include but disable by default, and only enable# per model by passing +dirty: true+ to +translates+.# dirty false# Column Fallback## Uncomment line below to fallback to original column. You can pass# +column_fallback: true+ to +translates+ to return original column on# default locale, or pass +column_fallback: [:en, :de]+ to +translates+# to return original column for those locales or pass# +column_fallback: ->(locale) { ... }+ to +translates to evaluate which# locales to return original column for.# column_fallback## Or uncomment this line to enable column fallback with a global default.column_fallbacktrue# Fallbacks## Uncomment line below to enable fallbacks, using +I18n.fallbacks+.# fallbacks## Or uncomment this line to enable fallbacks with a global default.fallbacks[{"es-cl"=>"es","es-co"=>"es","es-pe"=>"es","es-mx"=>"es","es-br"=>"es","en-cl"=>"en","en-co"=>"en","en-pe"=>"en","en-mx"=>"en","en-br"=>"en","pt-cl"=>"pt","pt-co"=>"pt","pt-pe"=>"pt","pt-mx"=>"pt","pt-br"=>"pt",}]# Presence## Converts blank strings to nil on reads and writes. Comment out to# disable.#presence# Default## Set a default translation per attributes. When enabled, passing +default:# 'foo'+ sets a default translation string to show in case no translation is# present. Can also be passed a proc.## default# Fallthrough Accessors## Uses method_missing to define locale-specific accessor methods like# +title_en+, +title_en=+, +title_fr+, +title_fr=+ for each translated# attribute. If you know what set of locales you want to support, it's# generally better to use Locale Accessors (or both together) since# +method_missing+ is very slow. (You can use both fallthrough and locale# accessor plugins together without conflict.)#fallthrough_accessors# Locale Accessors## Uses +def+ to define accessor methods for a set of locales. By default uses# +I18n.available_locales+, but you can pass the set of locales with# +translates+ and/or set a global default here.## locale_accessors## Or define specific defaults by uncommenting line belowlocale_accessors["es","es-cl","es-co","es-pe","es-mx","es-br","en","en-cl","en-co","en-pe","en-mx","en-br","pt","pt-cl","pt-co","pt-pe","pt-mx","pt-br",]# Attribute Methods## Adds translated attributes to +attributes+ hash, and defines methods# +translated_attributes+ and +untranslated_attributes+ which return hashes# with translated and untranslated attributes, respectively. Be aware that# this plugin can create conflicts with other gems.## attribute_methodsendend
The issue arises because there is a non-null constraint on the "valor" column in your "General" table, while simultaneously using translates :valor, type: :string in your model.
When employing Mobility with the backend :key_value configuration, the field for which you use translates are stored in one of Mobility's translation tables (string or text).
As a result, when you create/update an entry, the "valor" field is saved in Mobility's dedicated translation table, not in the "valor" column of your General table, leading to the error.
To resolve this, you should remove the "valor" column from your General table, or at least eliminate the non-null constraint.
Hi, Im setting up Mobility in our Rails app. It works when working in the console setting and saving data. But a problem appeared when we ran all our test suite. Consider a model called "General", we have a test helper that helps us to create one instance of this model with:
This helper fails in the
update!
method execution. I was debugging activerecord and ocurrs specifically in thesave!
internal execution of Rails. This is the full error:I tried different configurations of Mobility and in the model but nothing works :( These are the files currently:
General model
Mobility Config
Context
Gemfile.lock
Expected Behavior
The test should pass. I noticed that the value is actually saved, its like the column updates but the validation of PG fails.
Actual Behavior
The test fails, specifically, the update! execution of:
Possible Fix
The text was updated successfully, but these errors were encountered: