'Finding in array of active record results
I am using active record to create array.
users = User.all.to_a
now I want to later on find with in this array user id: 1
users.find(1)
but it is not giving result but returning everything. How can I search in this result array my selected user id. I can see users is a an array but with in array each record of User object.
If I do following
user.first
it return User object, but I want to search, how can I do it. I understand if I remove to_a then it will work but then it will create another sql query.
Solution 1:[1]
Since it is an array of objects, you should use find with block:
users.find { |user| user.id == 1 }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | potashin |
