# Has Many and finding unused options

**URL:** <https://www.ruby-forum.com/t/has-many-and-finding-unused-options/75447>\
**Category:** Rails\
**Created:** [November 9, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/has-many-and-finding-unused-options/75447 "2006-11-09T23:22:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [November 9, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/has-many-and-finding-unused-options/75447/1 "2006-11-09T23:22:46Z")

</div>

I’ve got a user model and a policy model. I’ve got the association  
set up so that both use the has\_many :through syntax. Given the  
scenario in editing a policy, a couple of users are already assigned.  
How can I figure out what users are not assigned to that policy  
already? Thanks

–  
Marlon

---

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [November 10, 2006, 4:55am UTC](https://www.ruby-forum.com/t/has-many-and-finding-unused-options/75447/2 "2006-11-10T04:55:45Z")

</div>

Marlon M. wrote:

> I’ve got a user model and a policy model. I’ve got the association  
> set up so that both use the has\_many :through syntax. Given the  
> scenario in editing a policy, a couple of users are already assigned.  
> How can I figure out what users are not assigned to that policy  
> already? Thanks
> 
> –  
> Marlon

Let’s say you have your policy object in @policy…

@unassigned\_users = User.find(:all).collect {|u| u unless  
@policy.users.include?(u)}

This line should populate @unassigned\_users as an array of User objects  
which are not currently linked to @policy.

How it works (in case you want to know)…  
1.The .collect method steps through a collection, executes the  
associated block of code for each item in the collection, and returns an  
array of all the values returned by the block. The |u| in the block maps  
to each user object returned by User.find(:all)

removed\_email\_address@domain.invalid returns the collection of User objects linked to  
@policy.

3.The .include? method returns true if the value in the parentheses (in  
this case a User object) exists in the collection on which you are  
executing the method (in this case, @policy.users). For our purposes  
here, if it returns true, the block does nothing, otherwise the block  
returns the User object, which ends up in the @unassigned\_users array.

c.

---

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [November 10, 2006, 5:33am UTC](https://www.ruby-forum.com/t/has-many-and-finding-unused-options/75447/3 "2006-11-10T05:33:18Z")

</div>

excellent…it never fails to amaze me how powerful ruby/rails really  
is.

On 11/9/06, Cayce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > –  
> > How it works (in case you want to know)…  
> > executing the method (in this case, @policy.users). For our purposes  
> > here, if it returns true, the block does nothing, otherwise the block  
> > returns the User object, which ends up in the @unassigned\_users array.
> 
> c.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Marlon

---

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [November 10, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/has-many-and-finding-unused-options/75447/4 "2006-11-10T14:54:29Z")

</div>

Yeah, block programming (something I’d never heard of before Ruby/Rails)  
is pure gold.

c.
