# Proxies and Arrays

**URL:** <https://www.ruby-forum.com/t/proxies-and-arrays/125611>\
**Category:** Rails\
**Created:** [January 10, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/proxies-and-arrays/125611 "2008-01-10T16:43:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [January 10, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/proxies-and-arrays/125611/1 "2008-01-10T16:43:04Z")

</div>

Hi all!

I have the following situation:

class User \< ActiveRecord::Base  
belongs\_to :customer  
end

class Customer \< ActiveRecord::Base  
has\_many :events  
end

Now, I want to access all customer events from my user, so I added  
delegate :events, :to =\> :customer

This works great, but I would like to enforce some access control over  
my objects and would like to do this in the user model and not in a lot  
of places inside controllers. Normally, a user should only be able to  
CRUD his/her own events, but an admin should be able to access all. The  
following I tried in the User class:

alias real\_events events  
def events  
if is\_admin? then  
Event.find(:all)  
else  
real\_events  
end  
end

This works great for a normal user, since in controllers, I can do  
something like current.user.events.find(1). The problem however is that  
a real Array is returned when current\_user.is\_admin? and not a  
association proxy. When calling find() on a normal array, a block is  
expected.

Is there a way to overcome this issue or to structure it in a different  
way?

Thanks!

Wouter

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [January 11, 2008, 11:59am UTC](https://www.ruby-forum.com/t/proxies-and-arrays/125611/2 "2008-01-11T11:59:33Z")

</div>

Ok, I got it! Not the very best or generic solution, but it works for me  
🙂

alias real\_events events  
def events  
if is\_admin? then  
Event  
else  
real\_events  
end  
end

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [January 11, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/proxies-and-arrays/125611/3 "2008-01-11T12:20:20Z")

</div>

I was too fast… this isn’t the solution, since an admin user can’t do  
current\_user.events anymore…

back to the drawing board
