Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: testing if ActiveRecord results include a value

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

like image 329
FearMediocrity Avatar asked Feb 18 '09 00:02

FearMediocrity


2 Answers

users = User.find(:all, :limit => 10, :order => "rand()")
users.any? {|u| u.id == 3}
like image 123
Chuck Avatar answered Sep 24 '22 06:09

Chuck


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.

like image 22
Alex Wayne Avatar answered Sep 21 '22 06:09

Alex Wayne