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?”
Taylor S. wrote:
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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Taylor S. wrote:
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-----
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.