You should add resourcify
to your Post model and other all models you want to apply roles on, as described in readme.
So, your Post model should look like:
Post.rb
class Post < ActiveRecord::Base
resourcify
attr_accessible :user_id, :content
belongs_to :user
end
* EDIT *
You can get all users with role :admin using User.with_all_roles({:name => :admin})
I've created a vanilla project uses rolify with same User and Post model. Change Post model and add resourcify
, change User model and add has_many :posts
user = User.create(...)
user.add_role :admin
post = user.posts.create(...)
user2 = User.create(...)
user2.add_role(:moderator, post)
Seems working with these:
- User.with_all_roles({:name => :admin})
- User.with_all_roles({:name => :moderator, :resource => Post })
- User.with_all_roles({:name => :moderator, :resource => Post.first })