diff --git a/spec/integration/fields/polymorphic_assosiation_spec.rb b/spec/integration/fields/polymorphic_assosiation_spec.rb index ae2ff067f..c2ed1bac7 100644 --- a/spec/integration/fields/polymorphic_assosiation_spec.rb +++ b/spec/integration/fields/polymorphic_assosiation_spec.rb @@ -90,6 +90,30 @@ is_expected.to have_selector('select#team_comment_ids') end + + context 'with records in different models share the same id', js: true do + let!(:players) { [FactoryBot.create(:player, id: team.id, name: 'Jackie Robinson')] } + + it 'clears the selected id on type change', js: true do + visit edit_path(model_name: 'comment', id: comment.id) + select 'Player', from: 'comment[commentable_type]' + click_button 'Save' + is_expected.to have_content 'Comment successfully updated' + expect(comment.reload.commentable).to eq nil + end + + it 'updates correctly', js: true do + visit edit_path(model_name: 'comment', id: comment.id) + select 'Player', from: 'comment[commentable_type]' + find('input.ra-filtering-select-input').set('Rob') + page.execute_script("document.querySelector('input.ra-filtering-select-input').dispatchEvent(new KeyboardEvent('keydown'))") + expect(page).to have_selector('ul.ui-autocomplete li.ui-menu-item a') + page.execute_script %{[...document.querySelectorAll('ul.ui-autocomplete li.ui-menu-item')].find(e => e.innerText.includes("Jackie Robinson")).click()} + click_button 'Save' + is_expected.to have_content 'Comment successfully updated' + expect(comment.reload.commentable).to eq players[0] + end + end end context 'on show' do diff --git a/src/rails_admin/filtering-select.js b/src/rails_admin/filtering-select.js index 43bb11782..7695e545f 100644 --- a/src/rails_admin/filtering-select.js +++ b/src/rails_admin/filtering-select.js @@ -297,6 +297,7 @@ import I18n from "./i18n"; destroy: function () { this.input.remove(); this.button.remove(); + this.element.html($('')); this.element.show(); this.filtering_select.remove(); $.Widget.prototype.destroy.call(this); diff --git a/src/rails_admin/widgets.js b/src/rails_admin/widgets.js index 38c2ead68..aabd4833d 100644 --- a/src/rails_admin/widgets.js +++ b/src/rails_admin/widgets.js @@ -258,7 +258,6 @@ import I18n from "./i18n"; "options" ); object_select.data("options", selected_data); - object_select.val(""); object_select.filteringSelect("destroy"); object_select.filteringSelect(selected_data); });