# Children of childrens?

**URL:** <https://www.ruby-forum.com/t/children-of-childrens/119772>\
**Category:** Rails\
**Created:** [November 10, 2007, 9:46am UTC](https://www.ruby-forum.com/t/children-of-childrens/119772 "2007-11-10T09:46:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 10, 2007, 9:46am UTC](https://www.ruby-forum.com/t/children-of-childrens/119772/1 "2007-11-10T09:46:04Z")

</div>

Hi all!

I’m working on a role based auth system. Users have roles, roles have  
role\_items which enable access to a controller/action pair. So:

User  
-has\_and\_belongs\_to\_many :roles

Role  
-has\_and\_belongs\_to\_many :users  
-has\_many :role\_items

RoleItem  
-belongs\_to :role

so, using “user = User.find(1)”, I can access his roles on  
“user.roles” and do something like “user.roles.each do |role|” to go  
trough all roles a given user has. I would like to know if I can go 2  
levels down and do something like “user.roles.role\_items.each do |  
role\_item|”. This would help me simplify this:

```
user.roles.each do |role|
  role.role_items.each do |role_item|
    if role_item.controller == params[:controller] and

```

role\_item.action == params[:action]  
auth = true;  
end  
end  
end

I tried doing that but rails told me that “role\_items” was not defined  
for “user.roles”.

Also, if you have any tip on the whole role based auth system, I would  
love to hear your thoughts.

Thanks!
