Ruby on Rails: testing if ActiveRecord results include a value

After noon,

Suppose I put together a random selection of users:

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

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

I thought about Array.include? but it seems to me a dead end.

thanks

In JP

+6
ruby-on-rails activerecord
source share
3 answers
users = User.find(:all, :limit => 10, :order => "rand()") users.any? {|u| u.id == 3} 
+7
source share
 assert random_users.include?(User.find 3), "Not found!" 

Active recording objects are considered equal if they have the same identifiers. Array #include? evaluates objects defined by equality using the == method.

+2
source share

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

This will save you from completing another find.

0
source share

All Articles