Polymorphic association problem #370

aunderwo opened this Issue Apr 9, 2011 · 14 comments


None yet

8 participants

aunderwo commented Apr 9, 2011

When viewing a model that has a polymorphic association (via belongs to) I am getting the following problem:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

trace is

 /Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/fields/types/polymorphic_association.rb:33:in `associated_model_config'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/fields/types/polymorphic_association.rb:21:in `block in <class:PolymorphicAssociation>'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/base.rb:83:in `instance_eval'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/base.rb:83:in `block in register_instance_option'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/base.rb:57:in `block in register_instance_option'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/has_fields.rb:60:in `block in visible_fields'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/has_fields.rb:60:in `select'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/lib/rails_admin/config/has_fields.rb:60:in `visible_fields'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/app/controllers/rails_admin/main_controller.rb:339:in `list_entries'
/Users/anthony/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails_admin-1ee2f2af816b/app/controllers/rails_admin/main_controller.rb:36:in `list'

I'm sure this used to work fine in earlier versions of rails_admin :(

aunderwo commented Apr 9, 2011

This seems to be because in polymorphic_association.rb

the following method expects a parent_model
def associated_model_config
association[:parent_model].map{|type| RailsAdmin.config(type) }.select{|config| !config.excluded? }

inspecting my association this is nil
{:name=>:organisable, :pretty_name=>"Organisable", :type=>:belongs_to, :parent_model=>nil, :parent_key=>[:id], :child_model=>Calendar(id: integer, organisable_id: integer, created_at: datetime, updated_at: datetime, organisable_type: string, token: string), :child_key=>[:organisable_id], :options=>{:polymorphic=>true, :foreign_type=>"organisable_type"}}

kaapa commented Apr 9, 2011

I was able to reproduce by commenting out all has_many :comments, :as => :commentable in the dummy app. Do you have any models that define has_many :calendars, :as => :organisable?

It's still is a bug though. RailsAdmin shouldn't throw exception when no parent models are defined in polymorphic associations.

aunderwo commented Apr 9, 2011

No in the two models associated with calendars I have

has_one :calendar, :as => :organisable

I have no "has_many :calendars, :as => :organisable?" associations. Hope this helps and isn't too difficult a bug to squash. Thanks

kaapa commented Apr 9, 2011

Thank's for the info, I'll add a polymorphic has_one to dummy app and try to fix it soonish.

@kaapa kaapa was assigned Apr 10, 2011
giladbu commented Apr 29, 2011

having the same issue, let me know if you need more info

kaapa commented May 2, 2011

c948fe7 should fix this one. Besides supporting polymorphic has one associations in the child model (the belongs to side of association) that commit also hides the association in edit views for the parent models. This was due to the fact that AbstractObject would need specialized polymorphic update handling which is not implemented.

Should we try to implement a form input for the parent as well?

@kaapa kaapa closed this May 2, 2011

I'm seeing this same problem with rev b4a50b2.

The classes involved look like,

class Post < ActiveRecord::Base
  belongs_to :postable, :polymorphic => true

class User < ActiveRecord::Base
  has_many :posts, :as => :postable, :order => "created_at DESC"

class Team < ActiveRecord::Base
  has_many :users, :through => :team_user_associations, :uniq => true
  has_many :posts, :as => :postable, :order => "updated_at DESC"

The trace is:

/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/fields/types/polymorphic_association.rb:33:in `associated_model_config'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/base.rb:83:in `instance_eval'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/base.rb:83:in `visible'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/base.rb:57:in `send'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/base.rb:57:in `visible?'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/has_fields.rb:60:in `visible_fields'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/has_fields.rb:60:in `select'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/lib/rails_admin/config/has_fields.rb:60:in `visible_fields'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/app/controllers/rails_admin/main_controller.rb:339:in `list_entries'
/Library/Ruby/Gems/1.8/bundler/gems/rails_admin-66a0233bd810/app/controllers/rails_admin/main_controller.rb:36:in `list'

Viewing Users crashes as described. Viewing Teams works fine, as do other polymorphic references in our code. The references themselves are never editable, but that's fine by me: I just need to edit other fields on the user.

davidsf commented May 30, 2011

I have the same problem as billkauf. I think this bug should be reopened.


I'm also seeing this bug. It throws exceptions on both:

  • models which have belongs_to ..., :polymorphic => true (saying You have a nil object when you didn't expect it!),
  • and models which have has_many ..., :as => ... (saying undefined method klass for nil:NilClass).

As my key models has_many :comments, :as => :commentable, this issue makes rails_admin completely unusable for me. So I would be very grateful if you fixed this please.


As well, seeing this failure when doing 'Export Selected' from the list view on a single instance of an Inventory:

  has_many :inventory_items, :inverse_of => :inventory, :dependent => :destroy
  belongs_to :user, :inverse_of => :inventories

  belongs_to :inventory, :inverse_of => :inventory_items, :touch => true
  belongs_to :item, :polymorphic => true

How come this hasn't been re-opened?

@kaapa kaapa reopened this Jul 4, 2011
kaapa commented Jul 4, 2011

Sorry, hadn't noticed the activity in this thread...


I've done a bit of work on this. Can you guys retry with latest HEAD and tell me what happens?


It stopped crashing for me as of snagging HEAD this weekend. So, not sure if it was a fix in the Export or if I got an early look at your changes.


Ok I close it then. Thx for feedback.

@bbenezech bbenezech closed this Aug 4, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment