Rails - Given @comments how to get all but the last record
I have @comments which is either 0 -XXXX number of comme开发者_运维百科nts 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?
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]
how about popping off the last one using ruby, and returning a new array?
@comments.pop
精彩评论