New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements the has_cached_role? method to prevent N+1 issues #369

Merged
merged 1 commit into from Nov 6, 2015

Conversation

Projects
None yet
2 participants
@gabrielhilal
Contributor

gabrielhilal commented Oct 31, 2015

This method will take the preloaded roles and use ruby enumerable to check if user has the role without extra queries.

e.g:

@user.add_role :admin, Forum
@user.add_role :member, Forum

users = User.with_role(:admin, Forum).preload(:roles)
users.each do |user|
  user.has_cached_role?(:member, Forum) # no extra queries
end
@gabrielhilal

This comment has been minimized.

Show comment
Hide comment
@gabrielhilal
Contributor

gabrielhilal commented Nov 1, 2015

Implements the has_cached_role? and has_strict_cached_role? methods.
```ruby
@user.add_role :admin, Forum
@user.add_role :member, Forum

users = User.with_role(:admin, Forum).preload(:roles)
users.each do |user|
  user.has_cached_role?(:member, Forum) # no extra queries
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.

wldcordeiro added a commit that referenced this pull request Nov 6, 2015

Merge pull request #369 from gabrielhilal/has_cached_role
Implements the has_cached_role? method to prevent N+1 issues

@wldcordeiro wldcordeiro merged commit 1359f6f into RolifyCommunity:master Nov 6, 2015

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+9.7%) to 93.43%
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment