railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.9k stars 2.26k forks source link

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

Open kotsiuruba opened 1 year ago

kotsiuruba commented 1 year ago

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.

rebeccacrompton commented 6 months ago

@kotsiuruba did you ever come right with this issue?

We're having a similar issue after upgrading the rails admin gem from 2.2.1 to 3.1.2 that - when editing a record, all the has_one associations get cleared.

Specifically looks like behaviour changes in the gem upgrade from 2.2.1 to 3.0.0.beta

kotsiuruba commented 6 months ago

@kotsiuruba did you ever come right with this issue? We're having a similar issue after upgrading the rails admin gem from 2.2.1 to 3.1.2 that when editing all the has_one associations get cleared.

@rebeccacrompton unfortunately no. To prevent deletions we made that field "read only":

        field :notification do
          read_only true
        end
LukeWFerguson commented 5 months ago

Our product is encountering the same update issue, causing us to revert to version 2.2.1. However, we are also trying to upgrade to Rails 7, and version 2.2.1 isn't compatible with it. Consequently, we are stuck on Rails 6 until this issue is resolved or we move to ActiveAdmin or a similar alternative.