# Complex relationships

**URL:** <https://www.ruby-forum.com/t/complex-relationships/166690>\
**Category:** Rails\
**Created:** [April 28, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/complex-relationships/166690 "2009-04-28T16:11:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [April 28, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/complex-relationships/166690/1 "2009-04-28T16:11:04Z")

</div>

Currently, I have 3 models  
User  
Group  
Permission

Users and Groups have a HABTM relationship and Groups and Permissions  
have a HABTM relationship.

For purposes of authorization, I need to know if a user belongs to a  
particular group that has a particlar permission to determine if they  
can perform an operation.

What’s the best way to go about this?

---

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [April 29, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/complex-relationships/166690/2 "2009-04-29T18:08:50Z")

</div>

On Tue, Apr 28, 2009 at 10:10 AM, DVG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> particular group that has a particlar permission to determine if they  
> can perform an operation.

Have the user ask its groups if any of them have the permission in  
question:

target\_permission = Permission.find(…)  
user.groups.any? do |group|  
group.permissions.include?(target\_permission)}  
end
