Updating a record with has_one polymorphic association deletes this associated record from DB

Author: kotsiurubaCreated Oct 21, 2023Updated May 8, 2025

Describe the bug I have two models:

class RewardsProgram::Level < ApplicationRecord
    has_one :notification,
             dependent: :destroy,
             foreign_key: :notifiable_id,
             foreign_type: :notifiable_type,
             as: :notifiable,
             inverse_of: :notifiable,
             class_name: 'RewardsProgram::Notification'

and

class RewardsProgram::Notification < ApplicationRecord
  belongs_to :notifiable, polymorphic: true
end

rails_admin.rb configuration for the Level model

    config.model RewardsProgram::Level do
      edit do
        field :name
        field :notification 
      end
    end

Gemfile configuration

ruby '3.2.2'
gem 'rails', '~> 7.0.6'
gem 'rails_admin', '~> 3.1.2'

UI works pretty well and allows me to create a notification from the Edit Level page

Selection_098

But when I submit the edit level form assigned notification is going to be deleted from the DB

Selection_099

Expected behavior has_one association should be still assigned to the edited record

Add any other context about the problem here. When I tried to change association from has_one :notification to has_many :notifications with the same configuration editing works as expected.

Source: railsadminteam/rails_admin