1

Newbie here trying to get up and running with role_model and CanCan. I've added both gems to my Gemfile. Set them up and can verify that they are in fact working. However, when I try to add a new role to a user in my Dev DB, the rails console rolls back and saves nothing? I'm stumped, as I thought this would be a piece of cake....

Here's my code:

Model - User.rb

class User < ActiveRecord::Base
  include RoleModel

  attr_accessible :email, :username, :roles, :password, :password_confirmation

  has_secure_password
  validates :email, :username, :password, presence: true
  validates :email, :username, uniqueness: true

  roles :admin, :moderator, :developer, :user
end

Model - Ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    user.can :manage, :all if user.is? :admin
  end
end

The commands I run on the Rails console to add the proper role to the User:

admin_user = User.find(1)
admin_user.roles = [:admin]
admin_user.save

Rails then spits out:

User Exists (0.5ms)  SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'admin_user@example.com' AND "users"."id" != 1) LIMIT 1
  User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE ("users"."username" = 'admin_user' AND "users"."id" != 1) LIMIT 1
   (0.1ms)  ROLLBACK
 => false 
dnyce
  • 415
  • 1
  • 6
  • 20

1 Answers1

1

Looks like this had nothing to do with role_model. In pure newbie fashion I didn't realize that the user I was trying to save was running up against the "password" validation that I set in User.rb. I removed the

:password, presence: true

from that file as I'm not sure it's necessary due to previous validations. In any event, the issue was with errors that needed to be fixed.

dnyce
  • 415
  • 1
  • 6
  • 20