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? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Sep 10, 2011 at 21:36, jdkealy <jdkealy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 -- Main Web Site: davearonson.com | LOOKING FOR WORK, Programming Blog: codosaur.us | preferably RoR, in NoVa/DC; Excellence Blog: dare2xl.com | see main web site. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Sep 10, 2011 at 21:51, Dave Aronson <googlegroups2dave-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> Try this: > > u.matches.index { |match| match.user_id == 2 } == nilOn 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 -- Main Web Site: davearonson.com | LOOKING FOR WORK, Programming Blog: codosaur.us | preferably RoR, in NoVa/DC; Excellence Blog: dare2xl.com | see main web site. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Don''t forget about that Enumerable link: u.matches.any? {|match| match.id == 2}.should be_false -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Sep 11, 2011 at 16:35, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Don''t forget about that Enumerable link: > > u.matches.any? {|match| match.id == 2}.should be_falseAh, 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 -- Main Web Site: davearonson.com | LOOKING FOR WORK, Programming Blog: codosaur.us | preferably RoR, in NoVa/DC; Excellence Blog: dare2xl.com | see main web site. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.