Not sure if this is a bug, a feature, a missed doc or a bad config, however when I set all of the translation attributes to a null-sy value, then the translation record self-destructs.
I have the table-backend Mobility setup as follows:
schema.rb
create table "base_model", force: :cascade do |t|
t.boolean "is_animal"
t.boolean "is_vehicle"
...
end
create table "base_model_translation", force: :cascade do |t|
t.bigint "base_model_id", null: false
t.string "locale", null: false
t.boolean "is_animal_translation"
t.boolean "is_vehicle_translation"
...
end
base_model.rb
class BaseModel < ApplicationRecord
extend Mobility
has_many :translations,
class_name: "BaseModelTranslations",
autosave: true,
dependent: :destroy
translates :is_animal_translation
translates :is_vehicle_translation
end
base_model_translation.rb
class BaseModelTranslation < ApplicationRecord
belongs_to :base_model,
touch: true,
inverse_of: :translations
end
On the Rails console:
base_model = BaseModel.last
base_model.reload.translations # => []
# SET TO NULLSY
base_model.is_animal_translation = true
base_model.save
base_model.reload.translations
# => [
# <#BaseModel::Translation
# id: 1,
# is_animal_translation: true,
# is_vehicle_translation: nil,
# ...>]
base_model.is_animal_translation = false
base_model.save
base_model.reload.translations # => []
# SET TO PARTIAL NULLSY THEN FULL NULLSY
base_model.is_animal_translation = true
base_model.is_vehicle_translation = true
base_model.save
base_model.reload.translations
# => [
# <#BaseModel::Translation
# id: 1,
# is_animal_translation: true,
# is_vehicle_translation: true,
# ...>]
base_model.is_animal_translation = false
base_model.save
base_model.reload.translations
# => [
# <#BaseModel::Translation
# id: 1,
# is_animal_translation: false,
# is_vehicle_translation: true,
# ...>]
base_model.is_animal_translation = false
base_model.save
base_model.reload.translations # => []
Is this an intended behaviour, if so is there a bypass for this?
EDIT: Looks like this was an an intended behaviour (https://github.com/shioyama/mobility/blob/3cbeaeec8ef0a6d22ee05229141cd2b2bb33b17f/lib/mobility/backends/active_record/table.rb#L306), is there a bypass for this?