diff --git a/README.md b/README.md index 8cb93c6d..2403299c 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,33 @@ end This method should be used with caution. If you don't preload the roles, the `has_cached_role?` might return `false`. In the above example, it would return `false` for `@user.has_cached_role?(:member, Forum)`, because `User.with_role(:admin, Forum)` will load only the `:admin` roles. +### Scoping + +user.rb +``` +has_many :posts, through: :roles, source: :resource, source_type: :Post +has_many :moderated_posts, -> { where(roles: {name: :moderator}) }, through: :roles, source: :resource, source_type: :Post +``` +let's you do +``` +@user.posts +# => [ all the posts where the @user has a role ] +@user.moderated_posts +# => [ all the posts where the @user has a moderator ] +``` +post.rb +``` +has_many :users, through: :roles, class_name: 'User', source: :users +has_many :moderators, -> { where(:roles => {name: :moderator}) }, through: :roles, class_name: 'User', source: :users +``` +let's you do +``` +@post.users +# => [ all the users that have a role in this post ] +@post.moderators +# => [ all the users that have a moderator role in this post ] +``` + ## Resources * [Wiki](https://github.com/RolifyCommunity/rolify/wiki) @@ -274,7 +301,6 @@ This method should be used with caution. If you don't preload the roles, the `ha * [Tutorials](https://github.com/RolifyCommunity/rolify/wiki#wiki-tutorials): * [How-To use rolify with Devise and CanCanCan](https://github.com/RolifyCommunity/rolify/wiki/Devise---CanCanCan---rolify-Tutorial) * [Using rolify with Devise and Authority](https://github.com/RolifyCommunity/rolify/wiki/Using-rolify-with-Devise-and-Authority) - * [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/) ## Upgrade from previous versions