Quick question, for example if you have in your controller:
@users = User.all
for user in @users
blah blah blah
end
what is the best and most strait forward method to find out if you
have reached the last user in @users?
Quick question, for example if you have in your controller:
@users = User.all
for user in @users
end
what is the best and most strait forward method to find out if you
have reached the last user in @users?
On Oct 28, 2009, at 5:23 PM, h3avyarms wrote:
Quick question, for example if you have in your controller:
@users = User.all
for user in @users
if user == @users.last
end
blah blah blah
end
what is the best and most strait forward method to find out if you
have reached the last user in @users?
If you don’t want that check in there you could also do…
@users = User.all
last_user = @users.pop
for user in @users
#…
end
-philip
Thank you for the fast response, I was resorting to using if
statements in my current setup and the last_user = @users.pop solution
is much cleaner and should actuolly run faster for really large
loops.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs