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

Polymorphic association dropdown #3631

Closed
alina-ep opened this issue Jun 17, 2023 · 2 comments
Closed

Polymorphic association dropdown #3631

alina-ep opened this issue Jun 17, 2023 · 2 comments

Comments

@alina-ep
Copy link

Hi! I’m upgrading from rails_admin 0.0.5 to 3.1.2 and I’m having issues with polymorphic select forms/dropdown.

It worked great in rails 3, but not in rails 7.
I have these 4 models with polymorphic relations:

class Source < ActiveRecord::Base
  belongs_to :resource_item, polymorphic: true, inverse_of: :sources
end

class SourceItem < Source
end

class SheetMusicPiece < ActiveRecord::Base
  has_many :sources, as: :resource_item, inverse_of: :resource_item
end

class SourceCollection < Source
  has_many :sources, :as => :resource_item, :inverse_of => :resource_item
end

The way it used to work was that when I created a new SourceItem in the admin, it would generate a dropdown containing model names like "SheetMusicPiece" and "SourceCollection". Once I chose the model name, it would then generate a dropdown of instances of that model from which I could choose one to associate a new object with

This is a rails admin config in initializer:

config.model SourceItem do
    list do
      field :resource_item do
        help 'Required'
      end
    end
    edit do
      field :resource_item
    end
end

Expectation (rails 3):

expectations

Rails 7:

rails 7

My Gemfile:

source 'http://rubygems.org'
ruby '3.0.1'

gem 'activemerchant', '~> 1.99.0'
gem 'active_shipping'
gem 'cancan'
gem 'ckeditor', '4.3.0'
gem 'devise', '~> 4.9.2'
gem 'erubis'
gem 'exception_notification', '~> 4.5'
gem 'execjs'
gem 'jquery-rails', '~> 4.5.1'
gem 'jquery-ui-rails', '~> 6.0', '>= 6.0.1'
gem 'haml', '~> 6.1', '>= 6.1.1'
gem 'kaminari'
gem 'kt-paperclip', '~> 7.1', '>= 7.1.1'
gem 'mysql2' # required by thinking-sphinx
gem 'nio4r', '~> 2.4.0'
gem 'nokogiri', '~> 1.14', '>= 1.14.2'
gem 'paper_trail', '~> 14.0'
gem 'pg', '~> 1.4.6'
gem 'pdf-core', '~> 0.9.0'
gem 'pdf-reader'
gem 'prawn', '~> 2.4'
gem 'rack-ssl-enforcer'
gem 'rails', '~> 7.0', '>= 7.0.4.3'
gem 'rails_admin', '~> 3.0.0'
gem 'rails_admin_import', '~> 3.0.3'
gem 'responders', '~> 3.1'
gem 'rexml', '~> 3.2', '>= 3.2.5'
gem 'rubyzip', '~> 1.0'
gem 'sprockets-rails', '~> 3.4', '>= 3.4.2'
gem 'therubyracer'
gem 'thinking-sphinx', '~> 5.5', '>= 5.5.1'
gem 'tilt'
gem 'ttfunk', '~> 1.7.0'
gem 'unicorn-rails', '~> 2.2'
gem 'unicorn', '~> 6.1'

# Install iconv gem to prevent errors after Ruby 2.1 upgrade
gem 'iconv', '~> 1.0.4'

# Gems used only for assets and not required in production environments by default.
group :assets do
  gem 'sass-rails', "~> 5.1.0"
  gem 'coffee-rails', "~> 5.0.0"
  gem 'uglifier', '~> 3.0'
end

group :development, :test do
  gem 'byebug', '~> 10.0'
end

group :development do
  gem 'spring', '~> 3.1.1'
end

gem 'rack-utf8_sanitizer'
gem "sassc-rails"

I did a bit of digging and found that the potential issue is in rails_admin-3.1.2/lib/rails_admin/config/fields/types/polymorphic_association.rb and how the entire form is being constructed,
specifically when setting associated_model_config:

def associated_model_config
   @associated_model_config ||= association.klass.collect { |type| RailsAdmin.config(type) }.reject(&:excluded?)
end

when running this in the console for my polymorphic association:

irb(main):003:0> Source.reflect_on_association(:resource_item).klass
/home/user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-7.0.4.3/lib/active_record/reflection.rb:423:in `compute_class': Polymorphic associations do not support computing the class. (ArgumentError)

so when it comes to constructing the form in rails_admin-3.1.2/app/views/rails_admin/main/_form_polymorphic_association.html.erb

The type_collection variable is nil, which is why there are no dropdown options in the DOM.

So my question is: Is this a bug, or is the functionality I'm expecting no longer supported? Please, advice what should I do in this situation, I’m getting kind of desperate trying to make it work

Thanks!

@mshibuya
Copy link
Member

I could confirm the issue and made the fix as 2a89ebc.
Thank you for reporting, and also being a long-time user!

@alina-ep
Copy link
Author

Thank you!

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

2 participants