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

undefined method `klass' for nil:NilClass #1183

Closed
Olli opened this issue Jun 4, 2012 · 12 comments
Closed

undefined method `klass' for nil:NilClass #1183

Olli opened this issue Jun 4, 2012 · 12 comments

Comments

@Olli
Copy link

Olli commented Jun 4, 2012

I got this error on
rails_admin-0.0.3/app/views/rails_admin/main/index.html.haml where line #13 raised:

activerecord (3.2.3) lib/active_record/reflection.rb:385:in `block in source_reflection'
activerecord (3.2.3) lib/active_record/reflection.rb:385:in `collect'
activerecord (3.2.3) lib/active_record/reflection.rb:385:in `source_reflection'
activerecord (3.2.3) lib/active_record/reflection.rb:375:in `rescue in foreign_key'
activerecord (3.2.3) lib/active_record/reflection.rb:373:in `foreign_key'
rails_admin (0.0.3) lib/rails_admin/adapters/active_record.rb:253:in `association_foreign_key_lookup'
rails_admin (0.0.3) lib/rails_admin/adapters/active_record.rb:65:in `block in associations'
rails_admin (0.0.3) lib/rails_admin/adapters/active_record.rb:58:in `map'
rails_admin (0.0.3) lib/rails_admin/adapters/active_record.rb:58:in `associations'
rails_admin (0.0.3) lib/rails_admin/abstract_model.rb:29:in `block (2 levels) in polymorphic_parents'
rails_admin (0.0.3) lib/rails_admin/abstract_model.rb:28:in `each'
rails_admin (0.0.3) lib/rails_admin/abstract_model.rb:28:in `block in polymorphic_parents'
rails_admin (0.0.3) lib/rails_admin/abstract_model.rb:27:in `tap'
rails_admin (0.0.3) lib/rails_admin/abstract_model.rb:27:in `polymorphic_parents'
rails_admin (0.0.3) lib/rails_admin/adapters/active_record.rb:216:in `association_model_lookup'
rails_admin (0.0.3) lib/rails_admin/adapters/active_record.rb:63:in `block (2 levels) in associations'
rails_admin (0.0.3) lib/rails_admin/config/fields/types/polymorphic_association.rb:49:in `call'
rails_admin (0.0.3) lib/rails_admin/config/fields/types/polymorphic_association.rb:49:in `associated_model_config'
rails_admin (0.0.3) lib/rails_admin/config/fields/types/polymorphic_association.rb:19:in `block in <class:PolymorphicAssociation>'
rails_admin (0.0.3) lib/rails_admin/config/configurable.rb:68:in `instance_eval'
rails_admin (0.0.3) lib/rails_admin/config/configurable.rb:68:in `block in register_instance_option'
rails_admin (0.0.3) lib/rails_admin/config/configurable.rb:42:in `block in register_instance_option'
rails_admin (0.0.3) lib/rails_admin/config/proxyable/proxy.rb:31:in `method_missing'
rails_admin (0.0.3) lib/rails_admin/config/has_fields.rb:113:in `select'
rails_admin (0.0.3) lib/rails_admin/config/has_fields.rb:113:in `visible_fields'
rails_admin (0.0.3) lib/rails_admin/config/proxyable/proxy.rb:31:in `method_missing'
rails_admin (0.0.3) app/views/rails_admin/main/index.html.haml:13:in `__home_oli__rvm_gems_ruby_______p____global_gems_rails_admin_______app_views_rails_admin_main_index_html_haml__367596137_89088280'
@bbenezech
Copy link
Collaborator

Desactivate RailsAdmin until your ActiveRecord relations are properly set.

@Olli
Copy link
Author

Olli commented Jun 4, 2012

Thank you for this hint. I think it'll repair it :)

@Olli Olli closed this as completed Jun 4, 2012
@kellydunn
Copy link

@bbenezech could you elaborate a bit more on what it means to have the "ActiveRecord relations properly set", I'm also experiencing this and it's unclear as how to proceed.

@gabrielosorio
Copy link

@bbenezech hey, it's the same for me as with @kellydunn could you give some hints of what would be necessary to check? My associations are working.

Thanks in advance!

@mshibuya
Copy link
Member

@kellydunn @gabrielosorio
You should have some misconfigured(but never used) associations in your app.
ActiveRecord doesn't complain about improper associations unless they're used, but RailsAdmin scans all of associations in your models, thus misconfiguration leads to this error.

@gabrielosorio
Copy link

Thanks!

It was an issue with a has_many/through association. In my case the class_name was wrong on one of the sides.

Cheers!

facenord-sud added a commit to collaide/collaide that referenced this issue Jan 23, 2014
@submitteddenied
Copy link

Just in case someone finds this on Google like I did, my issue was a has_many/through association where the through part wasn't defined on the model yet.

#student.rb
has_many :enrollments  # <- missing this
has_many :classes, through: :enrollments

@johnrlive
Copy link

Since this is a associations error.

I fixed this by moving all my_model.rb files (except user.rb) out of the /app/models/ folder into a ~/Desktop/Temp_Folder/

Therefore ActiveRecord can't complain about what files are not there.

NOTE: After process this is complete make sure you move all your model.rb files back to /app/models/ folder. This is a work around it is better advised that at some point you fix your associations.

@noahmatisoff
Copy link

I had an association in another model that wasn't configured properly on the model it was associated to, solved this, and the error in RA went away. Thanks.

@hayesgm
Copy link

hayesgm commented Nov 10, 2015

Just hit this. It would be great to catch this error and report "You may have an invalid assoication in " error instead.

@apanzerj
Copy link

If there is one thing rails admin has taught me, it is that I'm bad at Associations. During this project, Rails_Admin has been like the canary in the coal mine and there are several commits that have laid claim to "fixing associations".

@vemv
Copy link

vemv commented Dec 13, 2015

Just hit this. It would be great to catch this error and report "You may have an invalid assoication in " error instead.

+1. A complete message could be "Association :foo for model Bar is invalid/incomplete. Ensure it's well configured in both parts of the association, including :foreign_key, :class_name and :inverse_of options."

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