I am a little puzzled by this.
My ultimate goal in a RoR project is to grab one random profile from my database.
I thought it would be something like:
@profile = Profile.find_by_user_id(rand(User.count))
He kept throwing an error because user_id0 does not exist, so I pulled out parts of it to check what was going on:
@r = rand(User.count)
<%= @r %>
This returns 0 every time. So what is going on? I registered 5 fake users and 5 related profiles to verify this.
If I take Profile.find_by_user_id(rand(User.count))and rewrite it as
Profile.find_by_user_id(3)
It works great.
User.countalso works. Therefore, I think that rand()it cannot accept an input other than a static integer.
I'm right? What's happening?
source
share