views:

421

answers:

3

Afternoon,

Lets say I have gather a random selection of users:

User.find(:all, :limit => 10, :order => "rand()")

Now from these results, I want to see if the user with the ID of 3 was included in the results, what would be the best way of finding this out?

I thought about Array.include? but that seems to be a dead end for me.

Thanks

JP

A: 
assert random_users.include?(User.find 3), "Not found!"

Active record objects are considered equal if they have equal ids. Array#include? respects the objects defined equality via the == method.

Squeegy
+2  A: 
users = User.find(:all, :limit => 10, :order => "rand()")
users.any? {|u| u.id == 3}
Chuck
A: 

User.find(:all, :limit => 10, :order => "rand()").any? { |u| u.id == 3 }

This will save you from doing another find.

runako