# Has\_many: get all objects, not only associated

**URL:** <https://www.ruby-forum.com/t/has-many-get-all-objects-not-only-associated/127756>\
**Category:** Rails\
**Created:** [February 1, 2008, 12:13am UTC](https://www.ruby-forum.com/t/has-many-get-all-objects-not-only-associated/127756 "2008-02-01T00:13:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [February 1, 2008, 12:13am UTC](https://www.ruby-forum.com/t/has-many-get-all-objects-not-only-associated/127756/1 "2008-02-01T00:13:40Z")

</div>

Hi all,

I have a model user.  
A user has\_many :roles

Roles: admin, super admin, user

This specific user has the admin role. But for a view I want to list all  
roles.  
Is it possible to do something like:

“user.roles\_all”? and get all the associated roles AND the ones not  
currently associated with the user?

So I can create some checkboxes with the ones checked that are  
associated.

I hope I’ve explained it right.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [February 1, 2008, 12:39am UTC](https://www.ruby-forum.com/t/has-many-get-all-objects-not-only-associated/127756/2 "2008-02-01T00:39:26Z")

</div>

Role.find(:all)

Leon B. wrote:

> “user.roles\_all”? and get all the associated roles AND the ones not  
> currently associated with the user?
> 
> So I can create some checkboxes with the ones checked that are  
> associated.
> 
> I hope I’ve explained it right.
> 
> Thanks in advance!

–  
[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:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [February 1, 2008, 12:41am UTC](https://www.ruby-forum.com/t/has-many-get-all-objects-not-only-associated/127756/3 "2008-02-01T00:41:47Z")

</div>

do this in the controller  
@roles = Role.find :all

and in the view use the @roles variable

On Feb 1, 12:13 am, Leon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [February 1, 2008, 9:30am UTC](https://www.ruby-forum.com/t/has-many-get-all-objects-not-only-associated/127756/4 "2008-02-01T09:30:40Z")

</div>

ajaxrussia wrote:

> do this in the controller  
> @roles = Role.find :all
> 
> and in the view use the @roles variable
> 
> On Feb 1, 12:13 am, Leon B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

And how would I now then which roles are associated with the user? That  
line only gets all the roles.
