Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kotsiuruba opened this issue Oct 21, 2023 · 3 comments

Comments

@kotsiuruba
Copy link

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
Copy link

rebeccacrompton commented May 14, 2024

@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
Copy link
Author

@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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants