# Removing all the objects in one collection from another

**URL:** <https://www.ruby-forum.com/t/removing-all-the-objects-in-one-collection-from-another/84460>\
**Category:** Rails\
**Created:** [February 4, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/removing-all-the-objects-in-one-collection-from-another/84460 "2007-02-04T21:50:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [February 4, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/removing-all-the-objects-in-one-collection-from-another/84460/1 "2007-02-04T21:50:09Z")

</div>

Say I have the following code:

@product = Product.find(1)  
@features = Feature.find(:all)  
@usedFeatures = @product.features

I’d like to have a variable containing all the objects in @features  
minus the ones in @usedFeatures. I’ve tried using the enumerable  
functions with limited success. Can anyone help me out here? Thanks!

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [February 4, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/removing-all-the-objects-in-one-collection-from-another/84460/2 "2007-02-04T22:17:46Z")

</div>

Pieter M. wrote:

> Say I have the following code:
> 
> @product = Product.find(1)  
> @features = Feature.find(:all)  
> @usedFeatures = @product.features
> 
> I’d like to have a variable containing all the objects in @features  
> minus the ones in @usedFeatures. I’ve tried using the enumerable  
> functions with limited success. Can anyone help me out here? Thanks!

@otherFeatures = @features - @usedFeatures

Check out the documentation for class Array:  
[http://www.ruby-doc.org/core/classes/Array.html](http://www.ruby-doc.org/core/classes/Array.html)

If you want to do it in one db query:

@otherFeatures = Feature.find(:all, :conditions =\> [“product\_id != ?”,  
@product.id])

–  
Josh S.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

---

<div class="post-metadata">

**Author:** ![feanix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de0a6/32.png) [@feanix](https://www.ruby-forum.com/u/feanix)\
**Post date:** [February 5, 2007, 2:56am UTC](https://www.ruby-forum.com/t/removing-all-the-objects-in-one-collection-from-another/84460/3 "2007-02-05T02:56:07Z")

</div>

> @otherFeatures = @features - @usedFeatures
> 
> Check out the documentation for class Array:  
> [class Array - RDoc Documentation](http://www.ruby-doc.org/core/classes/Array.html)
> 
> If you want to do it in one db query:
> 
> @otherFeatures = Feature.find(:all, :conditions =\> [“product\_id != ?”,  
> @product.id])
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

This doesnt seem to work. I get an error saying it can’t convert Feature  
into an array. Any ideas?
