Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Rails Authorization plugin error
8 Posts
Rails Authorization plugin error

I'm trying to integrate the rails-authorization plugin into my current project to handle access levels to various parts of my current project, and I've run into a problem I can't seem to solve.

Every time my controllers try to do anything with the user model (set up with acts_as_authorized_user) it falls over saying that the const 'User#RolesUser' is missing. I can't find any reference to this in any documentation, or in the code for the plugin.

If anyone has come across this issue before, and/or can think of a solution to it, it would be greatly appreciated as I am stuck until I can get this working.

Thanks all

Hi David

         After installing the plugin, do you have the tables roles , users, role_user in your database ?


Yeah, I've run the migration created by the plugins generator, which creates roles and roles_users. The users table is the one from restful authentication which is working fine (logins are working perfectly, apart from a lack of email server on my dev machine at the moment :))


              I think you have not set the  roles for the user, whether your roles-user table is emty?


I have the same problem as original author - I'm writing new rails 2.2.2 application and every use of has_role? or has_role method end up with 'uninitialized constant User::RolesUser'. I've copy&paste migrations from older projects, so database have to be correct. But what's interesting to me, the code fail on constant RolesUser, but it's HABTM relationship so, by mine (basic) knowledge of rails, it should look for RolesUsers (plural form). Do anybody know if convenctions for M:N relationship has changed in rails 2.2.2? Or what possible can be wrong? :-)

Dwija, I get the error when I attempt to add roles, as well as when checking them.

At Jaroslav, I think you may be onto something there. Checking the plugin, they aren't HABTM relationships, they are 'has_many' and 'has_many :through => ' relationships. It didn't seem to work when I just changed the table name to 'role_users', but I'm going to try playing around with the relationship names as well.

That seems to have fixed it. In the library files, I replaced all references to ':roles_users' with ':role_users', renamed the table to 'role_users', and updated the Role model to use a has_many relationship called ':role_users' and I have started getting permission denied errors rather than AuthorizationParseError exceptions :)

David: Thanks, you have an exelent point. I've change relationship to HABTM and it's working now (yes - I hack the plugin). So this bring me to an idea why it's not working, as you wrote there is 'has_many :roles, :through => :roles_users', but rails are looking for constant RolesUser, so I change my original question - Do anybody know if convenctions for has_many :through relationship has changed in rails 2.2.2? :-)

Patch: --- a/vendor/plugins/rails-authorization-plugin/lib/publishare/object_roles_table.rb +++ b/vendor/plugins/rails-authorization-plugin/lib/publishare/object_roles_table.rb @@ -11,8 +11,10 @@ module Authorization

   module ClassMethods
     def acts_as_authorized_user(roles_relationship_opts = {})
  •    has_many :roles_users, :dependent => :delete_all
  •    has_many :roles, :through => :roles_users

    +# 20090121:Jarcec-Hack to get it work on rails 2.2.2 +# has_many :roles_users, :dependent => :delete_all +# has_many :roles, :through => :roles_users

  •     has_and_belongs_to_many :roles
       include Authorization::ObjectRolesTable::UserExtensions::InstanceMethods
       include Authorization::Identity::UserExtensions::InstanceMethods   # Provides all kinds of dynamic sugar via method_missing

Note: I'm using just has_role, has_role?, and has_no_role methods, others may not work correctly!

8 Posts
Login to add your message