# Around\_filter and with\_scope

**URL:** <https://www.ruby-forum.com/t/around-filter-and-with-scope/182391>\
**Category:** Rails\
**Created:** [January 27, 2010, 12:33pm UTC](https://www.ruby-forum.com/t/around-filter-and-with-scope/182391 "2010-01-27T12:33:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 27, 2010, 12:33pm UTC](https://www.ruby-forum.com/t/around-filter-and-with-scope/182391/1 "2010-01-27T12:33:34Z")

</div>

i got two controller (with restful actions) where my code is quite  
ugly and not very dry. every action looks quite like this:  
if @logged\_user.has\_role?(“admin”)  
User.find(params[:id)  
else  
@logged\_user.group.user.find(params[:id])

this is a security check that enforce a simple spec: normal user  
should read/write information only about their group’s users, but  
“admin” users can read/write about all users.  
The other controller is quite similar, just on another model.

trying to refactor a was reading about using a with\_scope in an  
around\_filter.  
this seems to work and the code gets a lot smaller. But it seems to be  
a deprecated practice…  
What is the “rails way” to accomplish this?  
thanks.

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 27, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/around-filter-and-with-scope/182391/2 "2010-01-27T13:06:20Z")

</div>

On 27 Jan 2010, at 11:32, eugenio wrote:

> The other controller is quite similar, just on another model.
> 
> trying to refactor a was reading about using a with\_scope in an  
> around\_filter.  
> this seems to work and the code gets a lot smaller. But it seems to be  
> a deprecated practice…  
> What is the “rails way” to accomplish this?  
> thanks.

one way would be to have a before filter that looked like

def get\_user\_to\_edit  
if @logged\_user.has\_role?(“admin”)  
@user = User.find(params[:id)  
else  
@user = @logged\_user.group.user.find(params[:id])  
end  
end

and then @user is ready for you in your actions.

Fred
