Re: database query and loops

Philip Hallstrom
2009-10-29T00:33:12Z

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
   # do something with the last user
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

# do something with last_user

-philip