searching the an array of objects

Hi,

I'm trying to write some tests.

I have a complicated query for "matches"

one criteria is that one's matches should not contain someone from
your blocked list.

in testing the matches method, i'm not sure how to test this.

u = User.find(1)
u.blockUser(2)
u.matches.where(:user_id=>2).should be_empty

however where is an invalid method for an array, i see... And I can't
seem to do anything with the find method. Any ideas here?

Try this:

  u.matches.index { |match| match.user_id == 2 } == nil

-Dave

On rereading this and then seeing the thread on asking questions,
methinks I ought to talk a bit about how I came up with this, and how
the rest of you can do likewise when stuck in a simliar situation as
the OP. As you may recall, he wanted to use find, but the method
returned an Array, which doesn't have a find method.

I figured, there's *got* to be *some* useful method on Array. There
wasn't one that I immediately recalled offhand. So I went to the docs
for the Array class, and looked at the methods. Of course there were
things like map/collect, that could be used to loop over it and return
true if it found a match or false if it hit the end, or select that
could be used to construct an array of "hit" elements. But I figured
there was probably a cleaner solution, that would still be clear and
concise, as a one-liner. I didn't know index would take a block...
and stumbling across that was the key.

Upshot: if you want to use method X, that works on class A, but you're
stuck with class B, take a quick stroll through the methods on B, and
see if there's something that can at least be used in a similar way.
If not, maybe there's even something better -- you don't have to solve
all similar problems the exact same way.

-Dave

Don't forget about that Enumerable link:

u.matches.any? {|match| match.id == 2}.should be_false

Ah, THAT'S where that was hiding! Something in the back of my brain
was saying "any", but my Ruby-fu is a touch out of date so I didn't
dredge up immediately where to look to verify that.

So, revised upshot: do the procedure I recommended on ALL applicable
classes! Start from the current thing and work up the inheritance
chain until you find something suitable.

Thanks,
Dave