diff --git a/lib/mobility/backends/table.rb b/lib/mobility/backends/table.rb index c48440b7..19339ba9 100644 --- a/lib/mobility/backends/table.rb +++ b/lib/mobility/backends/table.rb @@ -85,7 +85,8 @@ module Table # @!group Backend Accessors # @!macro backend_reader def read(locale, **options) - translation_for(locale, **options).send(attribute) + translation = translations.in_locale(locale) + translation.send(attribute) if translation end # @!macro backend_writer diff --git a/spec/mobility/backends/active_record/table_spec.rb b/spec/mobility/backends/active_record/table_spec.rb index 774768e6..bb51673a 100644 --- a/spec/mobility/backends/active_record/table_spec.rb +++ b/spec/mobility/backends/active_record/table_spec.rb @@ -109,6 +109,20 @@ end end + it "does not prevent saving when a nil translation is generated and invalid" do + Article::Translation.validates :title, presence: true + + Mobility.locale = :en + article = Article.new(title: "New Article") + expect(article.save).to eq(true) + + Mobility.locale = :ja + article.title + + Mobility.locale = :en + expect(article.save).to eq(true) + end + it "removes nil translations when saving persisted record" do Mobility.locale = :en article = Article.create(title: "New Article")