Thursday, November 29, 2007

Authorization plugin + restful_authentication + rails 1.2.5 = no worky

In a recent application with rails 1.2.5, I started to set up user authentication and authorization using Restful Authentication and the Authorization plugins.

When I added 'acts_as_authenticated_user' to the user.rb model generated by Restful Authentication, the generated specs for Restful Auth started failing with things like this:

NoMethodError in 'User should create user'
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
spec/models/user_spec.rb:99:in `create_user'
spec/models/user_spec.rb:12:
spec/models/user_spec.rb:11:
spec/models/user_spec.rb:7:

After a little investigation, I discovered at some point since the last time I had used these two plugins together, rails started to not like using attr_accessible and attr_protected in the same ActiveRecord class. This was the issue, as Restful Auth was declaring attr_accessible in the user model and Authorization was declaring attr_protected in lib/publishare/object_roles_table.rb line 15.

Commenting out the attr_protected :role_ids in the acts_as_authenticated_user method in object_roles_table.rb fixed the problem.