Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Given @comments how to get all but the last record

I have @comments which is either 0 -XXXX number of comments from the DB.

I need this @comments as is as it is used in multiple places.

In one place I need @comments but minus the last record, if any.

How can I do that? w/o rehitting the db?

like image 857
AnApprentice Avatar asked Feb 25 '23 09:02

AnApprentice


1 Answers

As already mentioned, @comments.pop will do the job. Another option is to use a range to select which items from the array to use:

@comments[0..-2]
like image 154
idlefingers Avatar answered Mar 07 '23 04:03

idlefingers