Skip to content

Instantly share code, notes, and snippets.

@daniely
Created May 13, 2015 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniely/28be3922ba8081410e6f to your computer and use it in GitHub Desktop.
Save daniely/28be3922ba8081410e6f to your computer and use it in GitHub Desktop.
rolify with fixtures

I'm using rails 4.2 and had a tough time with rolify and rails fixtures. After some mucking I finally got it working. Posting here incase someone else needs to do something similar.

  1. Create a UsersRole model in models/users_role.rb
# pretty much exclusively created for our fixtures to work
class UsersRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end
  1. Create fixtures like so:
# users.yml
some_user:

# roles.yml - key here is to NOT include "resource" 
admin_role:
  name: :admin

# users_roles.yml
users_roles1:
  user: some_user
  role: admin
  1. Add the fixtures to your tests
# in some test file
fixtures :users, :roles, :users_roles

users(:some_user).has_role? :admin # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment