Re: database query and loops

h3avyarms
2009-10-29T00:42:35Z

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.

On Oct 28, 8:33 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org>
wrote:
> 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