Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise Invitable Rails Admin conflict

Rails Admin was working fine until I installed Devise_Invitable. Now, when I click on Users in Rails Admin I get the following error:

NoMethodError in RailsAdmin::MainsController#list

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

The other models work fine.

I have a hunch that this might be part of the trouble:

https://github.com/sferik/rails_admin/issues/370

as this is part of the devise_invitable

 belongs_to :invited_by, :polymorphic => true

It says the issue is fixed but is there any way I don't have the most recent rails admin?

gemfile

gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'

Thanks for any ideas.

like image 955
LennonR Avatar asked Feb 25 '23 04:02

LennonR


2 Answers

The answer that has you add has_many :invitees, :class_name => self.class.name, :as => :invited_by to the User model works to get rails_admin working, but I ran into problems deleting and editing. The solution is to change the line you add to the User model to:

has_many :invitees, :class_name => "User", :as => :invited_by
like image 80
CuriousYogurt Avatar answered Mar 06 '23 02:03

CuriousYogurt


Unfortunately, the stacktrace you got (and the one in that GitHub issue) is very common in rails_admin—all it really means is that an association couldn't be automatically discovered. That fix, as you found, isn't applicable to the interaction with devise_invitable.

The NoMethodError happens because the devise_invitable gem (as of 0.5.0) only declares the belongs_to half of the invited_by relationship. rails_admin isn't able to fully understand this partially-declared relationship and barfs.

We were able to fix this in our app by adding the following to our Devise model:

class User < ActiveRecord::Base

  # Create an explicit User.invitees => [User, User, …] relationship
  # so that rails_admin can correctly discover this relationship.
  has_many :invitees, :class_name => self.name, :as => :invited_by       

  …

end

If you'd rather not keep track of the inviter/invitee relationship at all, you could fork the devise_invitable gem and remove that functionality.

like image 42
Phil Calvin Avatar answered Mar 06 '23 01:03

Phil Calvin