# Moving Permission Logic to the Model

**URL:** <https://www.ruby-forum.com/t/moving-permission-logic-to-the-model/173395>\
**Category:** Rails\
**Created:** [August 7, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/moving-permission-logic-to-the-model/173395 "2009-08-07T23:15:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 7, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/moving-permission-logic-to-the-model/173395/1 "2009-08-07T23:15:12Z")

</div>

Ideally what I would like to have happen:  
Contact.find (and .find\_by\_xyz, etc) will never return a contact  
unless contact.group is IN  
current\_user.contact.group.supergroup.groups

Scenario:  
User belongs to a Contact.  
Contact belongs to a Group.  
Group belongs to a SuperGroup.

Contact has permissions to access the directory listing of other users  
of the same SuperGroup, but no one else.

There is never a case where Contact.find (or its derivatives

- .find\_by\_xyz) is called unless a User, which has\_one Contact is  
logged in.

What I want assistance with:  
How can I put this constraint into the model?  
There will never be an exception to this rule so I don’t want to use a  
named scope unless that’s the only way. I want to override every  
default find for this class, but I don’t know how.

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 7, 2009, 11:19pm UTC](https://www.ruby-forum.com/t/moving-permission-logic-to-the-model/173395/2 "2009-08-07T23:19:18Z")

</div>

2009/8/7 CoolAJ86 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Group belongs to a SuperGroup.  
> How can I put this constraint into the model?  
> There will never be an exception to this rule so I don’t want to use a  
> named scope unless that’s the only way. I want to override every  
> default find for this class, but I don’t know how.

How about default\_scope?

Colin

---

<div class="post-metadata">

**Author:** ![coolaj86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@coolaj86](https://www.ruby-forum.com/u/coolaj86)\
**Post date:** [August 8, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/moving-permission-logic-to-the-model/173395/3 "2009-08-08T17:42:09Z")

</div>

That is exactly what I was looking for.

Thanks so much Colin.
