0

I want to bring CanCanCan to my Rails app but I fail at read the role attribute of my user-class. I only need a "one role per user" solution so I added a column 'role' to my user model and migrated it successfully but when I try to read the attribute, its nil...

I've created the following in my user class:

class User < ActiveRecord::Base

    ## User Model
    ROLES = %i[admin default]
    ...
    attr_accessor :role
    ...
end

When I try the following on the Rails console:

irb(main):001:0> user = User.find(1)
User Load (1.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
=> #<User id: 1, username: "ofhouse", email: "test@example.com", ... , role: "admin">
irb(main):002:0> user.role
=> nil
irb(main):003:0> user.username
=> "ofhouse"

I got nil for the user's role, while other attributes are accessible?

ofhouse
  • 3,047
  • 1
  • 36
  • 42

1 Answers1

2

Remove the attr_accessor for role, it's shielding the role attribute generated by AR automatically.

Jiří Pospíšil
  • 14,296
  • 2
  • 41
  • 52