0
votes

I am using gem Rolify 3.2.0 to manage user roles.

When I add a non-scoped role as such, everything works. Using 'rails console':

u = User.first
u.roles
 => []
u.add_role :admin
u = User.first
u.roles.first.name
 => "admin"

Great,but when I try to do the same thing but with a role scoped to a resource, it does not persist. Again using 'rails console':

u = User.first
u.roles
 => []
u.add_role :admin, Agency.first
u.roles.first.name
 => "admin"
u.roles.first.resource_type
 => "Agency"
u.save!
u = User.first
u.roles
 => []

So, everything is fine with the local user, but when I re-query the model, then role has not been persisted?!

My Role model is straight from the rolify generator:

class Role
  include Mongoid::Document

  has_and_belongs_to_many :users
  belongs_to :resource, :polymorphic => true

  field :name, :type => String
  index({ :name => 1 }, { :unique => true })

  index({
    :name => 1,
    :resource_type => 1,
    :resource_id => 1
  },
  { :unique => true})

  scopify
end

Please help :)

1

1 Answers

0
votes

The problem was in the model, there was two unique indexes on the name field. I removed the first index and now everything works perfectly. I also sent a pull request to the rolify repo to see if we can fix this for others.