shioyama / mobility

Pluggable Ruby translation framework
MIT License
1.01k stars 82 forks source link

validates presence: true failing with cache active #623

Open alejanderl opened 1 year ago

alejanderl commented 1 year ago

Hey, thanks for the library and great work.

We are implementing multi language with Mobility and we found this issue:

Context

We are using Mobility with a Rails 7 application and the v1.3.0.rc1

Expected Behavior

Deactivating the cache is making everything to work as expected

class SomeClass < ApplicationRecord
      translates :name, backend: :jsonb

      validates :name, presence: true
end

my_record = SomeClass.find(1)
#<SomeClass name: "Legolas" ... >  

my_record.update(name: "")
# false

my_record.valid?
# false

my_record.update(name: "Aragorn")
# true

Actual Behavior

With the cache active in the Mobility config we experienced this behaviour;

class SomeClass < ApplicationRecord
      translates :name, backend: :jsonb

      validates :name, presence: true
end

my_record = SomeClass.find(1)
#<SomeClass name: "Legolas" ... >  

my_record.update(name: "")
# true

my_record.valid?
# false

my_record.update(name: "Aragorn")
# false
tot-ioki commented 1 year ago

We're also experiencing this issue when using Rails 7 in conjunction with mobility 1.3.0.rc1.

@alejanderl did you find a way to work around this without having to disable the caching?

shioyama commented 7 months ago

Hi folks. I really need a failng test for this.

doits commented 7 months ago

(Not a solution, but just want to give the hint that – if I am not mistaken – validates :name, presence: true will validate depending on the current ::I18n.locale which might be different for different requests (and will take into account plugins like fallback etc). Maybe this is what you want, but it could happen that a record is valid with ::I18n.locale = :en but invalid with ::I18n.locale = :de, just to keep that in mind.)