Removing all the objects in one collection from another

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!

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

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/

@otherFeatures = @features - @usedFeatures

Check out the documentation for class Array:
class Array - RDoc Documentation

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/

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