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 problem #370

Closed
antunderwood opened this issue Apr 9, 2011 · 14 comments
Closed

Polymorphic association problem #370

antunderwood opened this issue Apr 9, 2011 · 14 comments
Assignees
Milestone

Comments

@antunderwood
Copy link

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 :(

@antunderwood
Copy link
Author

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? }
end

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
Copy link
Collaborator

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.

@antunderwood
Copy link
Author

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
Copy link
Collaborator

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.

@ghost ghost assigned kaapa Apr 10, 2011
@giladbu
Copy link

giladbu commented Apr 29, 2011

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

@kaapa
Copy link
Collaborator

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 as completed May 2, 2011
@billkauf
Copy link

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/fields/types/polymorphic_association.rb:21
/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
Copy link

davidsf commented May 30, 2011

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

@RKushnir
Copy link

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.

@crosebrugh
Copy link
Contributor

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

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

InventoryItem
  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
Copy link
Collaborator

kaapa commented Jul 4, 2011

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

@bbenezech
Copy link
Collaborator

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

@crosebrugh
Copy link
Contributor

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.

@bbenezech
Copy link
Collaborator

Ok I close it then. Thx for feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants