开发者

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

开发者 https://www.devze.com 2023-02-05 04:00 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消