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
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
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:
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 withThis is a rails admin config in initializer:
Expectation (rails 3):
Rails 7:
My Gemfile:
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 settingassociated_model_config
:when running this in the console for my polymorphic association:
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 isnil
, 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!