Create mirror records for translations using Mobility gem with Rails
01:20 24 Aug 2026

a class defines

  before_validation :set_default_name_locale

  private

  def set_default_name_locale
    Mobility.with_locale(I18n.default_locale) do
      self.name_locale = name if name_locale.blank?
    end
  end

but fails the following test

  test "sets name_locale to name in default locale when name_locale is blank" do
    record = Minor.new( major_id: majors(:one).id, name: "Widget", label: 'widget', priority: 99 )

    assert record.valid?
    assert_equal "Widget", Mobility.with_locale(@default_locale) { record.name_locale }
  end

I wonder where the logic is wrong. Maybe the class should run an action self.name_locale = name if name_locale.blank? after the record is saved?

ruby-on-rails mobility