I have a collection of acts_as_ferret search results that I need to trim based on an attribute. I''ve been trying to iterate through the collection but haven''t been able to make this work. Here is what I have tried unsuccessfully: @people.each {|person| person.clear if person.privacy_type == "private"} Unfortunately Class Person does not respond to clear since it is an AR object not a hash. Even if it were an hash, I feel like I need to perform the operation on the COLLECTION of hashes @people anyways. However, checking @people with <%= @person.class %> tells returns an unhelpful ''NilClass.'' How can I iterate through @people and delete all the ones with privacy_type == "private?" -- 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 -~----------~----~----~----~------~----~------~--~---
Taylor Strait wrote:> I have a collection of acts_as_ferret search results that I need to trim > based on an attribute. I''ve been trying to iterate through the > collection but haven''t been able to make this work. Here is what I have > tried unsuccessfully: > > @people.each {|person| person.clear if person.privacy_type == "private"} > > Unfortunately Class Person does not respond to clear since it is an AR > object not a hash. Even if it were an hash, I feel like I need to > perform the operation on the COLLECTION of hashes @people anyways. > > However, checking @people with <%= @person.class %> tells returns an > unhelpful ''NilClass.'' How can I iterate through @people and delete all > the ones with privacy_type == "private?" > >Something like: @people.find_all {|person| person.privacy_type != "private"} See: http://corelib.rubyonrails.org/classes/Enumerable.html#M002177 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Taylor Strait wrote:> I have a collection of acts_as_ferret search results that I need to trim > based on an attribute. I''ve been trying to iterate through the > collection but haven''t been able to make this work. Here is what I have > tried unsuccessfully: > > @people.each {|person| person.clear if person.privacy_type == "private"} > > Unfortunately Class Person does not respond to clear since it is an AR > object not a hash. Even if it were an hash, I feel like I need to > perform the operation on the COLLECTION of hashes @people anyways. > > However, checking @people with <%= @person.class %> tells returns an > unhelpful ''NilClass.'' How can I iterate through @people and delete all > the ones with privacy_type == "private?" >@people.delete_if{ |person| person.privacy_type == ''private'' } Look at the Ruby documentation for the Array class, and also Enumerable. http://www.ruby-doc.org/core/classes/Array.html http://www.ruby-doc.org/core/classes/Enumerable.html Zach -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFhdfKMyx0fW1d8G0RApnFAJ0QNsjLXJK5Iwjh8WsGVwNJwb/j7QCeM1LW k4Cic2WwYOg2WkFVnU/5X1w=vjSU -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Damn, I was close. I didn''t have a good grasp on what was actually inside @people which caused me to overlook using delete_if on it directly. Thanks to both of you for your help. -- 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 -~----------~----~----~----~------~----~------~--~---
Possibly Parallel Threads
- Rails 4 deprecation of in-place edit methods for collection associations
- delete_if doesn''t work for has_and_belongs_to_many
- How to list all models of an application?!?
- delete_if does not work on associations
- shouldn''t this work? - session[:array_of_objects].delete_if {|x| x.id == params[:id]}