Rails - Given @comments, how to get everything except the last entry

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

I need these @comments as is, as it is used in several places.

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

How can i do this? without db recovery?

+4
source share
2 answers

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

 @comments[0..-2] 
+4
source

how about popping up from the last using ruby ​​and returning a new array?

@comments.pop

+4
source

Source: https://habr.com/ru/post/1335753/


All Articles