My question is very simple, I have:
@users = User.first(100)
From the @users array, how can i get the user object with the id 50?
Use detect:
user = @users.detect { |u| u.id == 50 }
Though there are ways to fetch just one record (with id 50) if you don't need the remaining 99.
@users.find, how it works. But detect will iterate over the @users and when first element with matching condition is found, loop breaks and that element is returned.If you want to do an ActiveRecord find you can do:
@users = User.find_by_id(50)
Or if you want to do an Array find you can do:
@users.find_all { |user| user.id == 50 }