Skip to content
Florent Monbillard edited this page Aug 29, 2013 · 28 revisions

Frequently asked questions

When I start Rails using server, console, whatever, I get this error:

undefined local variable or method `rolify' for User:Class

If you are using Mongoid ORM, make sure you load rolify (using the rolify method) after Mongoid::Document include in the User class. For example:

class User
  include Mongoid::Document
  rolify
end

If you are using ActiveRecord, you should not get this error, please fill a bug


method_missing': undefined local variable or method scopify' for #Class:0x007ff921536890 (NameError)

If you get this error, you're probably using less-rails gem, there is a conflict in the less-rails railtie with rolify's one. In the meantime, add this to your application.rb:

require 'rolify/railtie'

Is it possible to manage 2 sets : Role and AdminRole ?

If you have 2 different User classes, let's say User and AdminUser, yes it is. To make it work, you need 2 Role classes (Role and AdminRole), so 2 tables in the database (so 2 migration files if you use ActiveRecord). To make it easier, just run the generator twice:

  • rails g rolify Role User
  • rails g rolify AdminRole AdminUser

In User class, you just have to put rolify method in it In AdminUser class, you would add: rolify :role_cname => "AdminRole" to make it work


Why rolify doesn't support ruby 1.8 ?

Mongoid >= 2.0 requires ruby 1.9, that's why rolify dropped ruby 1.8 support to maintain the dependency. Moreover, upcoming Rails 4.0 will drop ruby 1.8 support too. Although rolify doesn't necessarily use ruby 1.9 specific features or syntax, I quit enforcing the compatibility for these 2 reasons. In the case you have to use 1.8 for whatever reason, this repository hosts a fork of rolify supporting ruby 1.8. Don't expect it to be maintained heavily tho.


Does rolify support STI ?

If you use ActiveRecord STI and add rolify method only in the superclass, you should use becomes AR method before using any rolify commands, like this:

class User < ActiveRecord::Base
  rolify
  ...
end

class UserTypeOne < User
  ....
end

class UserTypeTwo < User
  ....
end

user = UserTypeTwo.find(1)
base_user = user.becomes(User)
base_user.add_role :admin 
Clone this wiki locally