FactoryGirl and Role_Model

If you're doing role-based authentication in your Rails applications, I highly encourage you to take a look at Martin Rehfeld's Role Model, which works really lovely with CanCan.

If you're using FactoryGirl for tests, though, it's a bit confusing to figure out how to properly assign roles to your fixtures. The answer is to use a callback, but if you try to make it work by following FactoryGirl's syntax, you might run into a FactoryGirl::AttributeDefinitionError. That's because the syntax isn't quite right if you're using Rails 3.2.

Here's the right way to do it:

About Me

I write about self-improvement at Think Really Hard.

I live in Brockville, Ontario, Canada, where I spend most of my time singing, cooking, and homeschooling my wonderful four-year-old Linus with my wife Becky.

Stay In Touch!