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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Josh Susser
2007-Feb-04 21:17 UTC
Re: Removing all the objects in one collection from another
Pieter Mans 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 Susser http://blog.hasmanythrough.com/ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pieter Mans
2007-Feb-05 01:56 UTC
Re: Removing all the objects in one collection from another
> @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 Susser > http://blog.hasmanythrough.com/This doesnt seem to work. I get an error saying it can''t convert Feature into an array. Any ideas? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---