Just added to project:
NoMethodError: undefined method `find_or_create_by' for #<Rolify::Adapter::ResourceAdapter:0x007fcc2fee7cf0>
Oh I know my issue.
I put resourcify on my user model as well. That blew it up. I wanted to do this because I wanted users to have roles over other users.
E.g User.find(1).add_role :manager, User.find(2)
I guess you can't do that. Anyone suggest a way to do this?
I've never tried it, but I think you can put rolify and resourcify methods in your User model, if you want to assign roles over other users. But don't forget rolify method as it provides the add_role method.
no you cannot have both ... I just tried it
creating an Admin w rolify only works,
creating an Admin w rolify and resourcify doesn't work
NoMethodError: undefined method find_or_create_by' for #<Rolify::Adapter::ResourceAdapter:0x007fdf61f1d180> from /Users/yves/.rvm/gems/ruby-1.9.3-p194@rails32/gems/rolify-3.2.0/lib/rolify/role.rb:12:inadd_role'
find_or_create_by' for #<Rolify::Adapter::ResourceAdapter:0x007fdf61f1d180> from /Users/yves/.rvm/gems/ruby-1.9.3-p194@rails32/gems/rolify-3.2.0/lib/rolify/role.rb:12:in
doesn't know if it's also raisng an error w Mongoid , but this error is clear enough w Activerecord
anyway to allow an (Admin / User) to have roles vs its sibblings ?
I tried to understand what could be wrong .. maybe I cannot do the follwoing :
I have 2 separate classes w roles : User ( frontoffice) and Admin ( for backoffice)
I splited the tables migrations into 3 :
I updated my Admin
class Admin < ActiveRecord::Base
doesn't run ...
#<Rolify::Adapter::ResourceAdapter:0x007fc3a8eb6fe0 @role_cname="Role", @user_cname="Admin">
NoMethodError: undefined method `find_or_create_by' for #Rolify::Adapter::ResourceAdapter:0x007fc3a8eb6fe0
this is the key point !! resourcify SHOULD BE defined BEFORE rolify
class Admin < ActiveRecord::Base
runs ... as in this case
#<Rolify::Adapter::RoleAdapter:0x007f844f880150 @role_cname="Role", @user_cname="Admin">
I want to do the same thing which is both resourcify and rolify my user model.
I tried putting resourcify above rolify but I got a:
SystemStackError: stack level too deep
maybe I should post to stackoverflow ;) I do have one difference in my case, im using mongodb and mongoid. I dont get any helpful messages just that one liner.
Ok, so ActiveRecord adapter looks like working if you declare resourcify and rolify in the good order but not Mongoid adapter. I'll investigate this.
Basically the problem happens when the method user.roles is called, and that happens when we try to add a new role, more specifically in lib/rolify/adapters/mongoid/role_adapter.rb
def add(relation, role)
relation.roles << role
When you add resourcify and rolify for the same model basically you are doing something like
field :name, type: String
And that seems to cause a great deal of confusion for our friend Mongoid. I think that Mongoid is defining a new method but with a wrong name which probably caused an infinite recursion and that's resulted in a SystemStackError: stack level too deep.
I'm not sure but maybe this is a Mongoid bug, but for sure we can do something to work around it.
I hope this was useful.
You are totally right. roles is already defined as an association in rolify method and resourcify defines another different roles association. I should add an argument to be able to tweak the association name in resourcify.
Thanks for the investigation.
edited specs for #111 and #102
added specs for mongoid
added optional argument on resourcify method to change the role assoc…
…iation name on the resource model
If you use master branch, you will be able to use rolify and resourcify in the User model class like this.
Please note that you have to use as argument of resourcify another association name than :roles because it's already used by rolify method. In that use case, this argument is mandatory.
This fix will be included in rolify 3.3 release
Still broken on 3.4 w/ rolify + resourcify User model. Removing resourcify from User model fixed it.
> u.add_role :admin
NoMethodError: undefined method `find_or_create_by' for #<Rolify::Adapter::ResourceAdapter:0x000001017dbde0>
from /Users/saizai/.rvm/gems/ruby-2.1.0/gems/rolify-3.4.0/lib/rolify/role.rb:12:in `add_role'