When I implemented some kind of posting function, for example, a function that displays new posts first, such as Twitter, there was a part where the idea was elaborated, so I will describe it as a reminder.
Here's what I was about to do:
I tried to sort the array containing the hash in descending order on the received view file side
But when I thought about it, I realized that it wasn't very maintainable.
Each function should be specialized, such as focusing on the function of display. The method is similar.
Therefore, it was necessary to do this when passing in a sorted state when passing
.
The following is an example. Added an option ".order ('created_at DESC')" when storing the data existing in the Item table in @items.
controller
def index
@items = Item.includes(:user).order('created_at DESC')
end
By doing this, I was able to control the display of the view file without modifying it.
Recommended Posts