开发者

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜