I have a feature called “Browse” that allows users to browse through random profiles. When a user clicks on “browse” they are immediately taken to a users profile that they are NOT already friends with. What should my controller look like?
Right now I’ve got:
def browse
@users = User.all.offset(rand(current_user.matches.count))
@users.each do |user|
if !current_user.friends.include?(user)
@user = user
return
end
end
end
However that doesn’t seem to be working. Any advice? I am admittedly bad with blocks, it seems!
You could try something like this
A better version would be
Also, if you are too concerned about performance, instead of using the offset technique, better use the randumb gem to fetch the random record. It uses database specific functions for selecting random records, if available.