# Has\_many :through question

**URL:** <https://www.ruby-forum.com/t/has-many-through-question/136944>\
**Category:** Rails\
**Created:** [May 7, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/has-many-through-question/136944 "2008-05-07T16:37:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [May 7, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/has-many-through-question/136944/1 "2008-05-07T16:37:45Z")

</div>

I am trying to setup the has\_many :through scheme but am having some  
difficulties.

The long story short I am building a simple ACL.  
I have Users, Roles and Permissions. Below is what I started with for  
associations.

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles  
end

class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
has\_and\_belongs\_to\_many :permissions  
end

class Permission \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles  
end

With this setup, everything was connected together and working fine.  
I would like to setup users having permissions through roles.

I tried:  
class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles  
has\_many :permissions, :through =\> :roles  
end

but that does not work. From what I have read, if I can get the  
through to work, I should beable to do something like  
@user.permissions.

How can I set this up so a user can have many roles, a role can have  
many permissions, a role can have many users, and a user can have  
permissions through roles?

Any suggestions would be very helpful.

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [May 7, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/has-many-through-question/136944/2 "2008-05-07T16:50:49Z")

</div>

has\_many :through only works though another has\_many, not a habtm. In  
order get a list of a users permissions, you’ll need to loop through  
their roles and collect all the permissions.

Unless you’re planning on multiple installs of your app each with  
different permission requirements, you probably don’t actually need to  
store permissions in the DB. If that’s the case, you might want to check  
out the GateKeeper plugin ( [http://gatekeeper.rubyforge.org/](http://gatekeeper.rubyforge.org/) ).

jm wrote:

> I would like to setup users having permissions through roles.
> 
> How can I set this up so a user can have many roles, a role can have  
> many permissions, a role can have many users, and a user can have  
> permissions through roles?
> 
> Any suggestions would be very helpful.
> 
> >

–

[http://www.5valleys.com/](http://www.5valleys.com/)

[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [May 7, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/has-many-through-question/136944/3 "2008-05-07T18:26:12Z")

</div>

Should I use this instead?

class User \< ActiveRecord::Base  
has\_many :roles  
has\_many :permissions, :through =\> :roles  
end

class Permission \< ActiveRecord::Base  
has\_many :roles  
has\_many :users, :through =\> :roles  
end

class Role \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :permission  
end
