使用 has_one 多态关联更新记录会从 DB 中删除此关联记录
作者: kotsiuruba创建于 2023年10月21日更新于 2025年5月8日
描述错误 我有两个模型:
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'
end和
class RewardsProgram::Notification < ApplicationRecord
belongs_to :notifiable, polymorphic: true
endrails_admin.rb 配置 Level 模型
config.model RewardsProgram::Level do
edit do
field :name
field :notification
end
endGemfile 配置
ruby '3.2.2'
gem 'rails', '~> 7.0.6'
gem 'rails_admin', '~> 3.1.2'UI 工作得很好,允许我在编辑级别页面上创建通知。
但是,当我提交编辑级别表单时,指定的通知将从数据库中删除
预期行为
has_one 关联应该仍然指定给已编辑的记录
请在此处添加有关问题的其他上下文。
当我尝试将关联从 has_one :notification 更改为 has_many :notifications 并使用相同的配置进行编辑时,编辑工作如预期的那样。
内容来源: railsadminteam/rails_admin