Hello! I will post for the first time this time. I began to think that it would be possible to fix my memory by outputting it as a memo for myself so that I would not have to worry about the same thing over and over again.
Even so, there may be an opportunity for someone to see it, so I would like to introduce myself a little. He started studying programming in earnest in mid-October 2020, and is currently utilizing a programming school, aiming to change jobs as an engineer in the middle of inexperience.
Well, on the main subject of this time!
First of all, I wondered what this word means, "youngest first". From the result, the answer was that the numbers were in ascending order, which would be 1.2.3.4 .... in the same sense. This youngest order is the general ascending order (asc), and vice versa is the descending order (desc).
If you compare it with the story of this ID, -Ascending order: In ascending order of ID. Old articles come up. ・ Descending order: In descending order of ID. A new article comes first.
This time, I used the method described in the controller, so I will leave it.
#Default notation
@books = Book.all
@books = Book.all.order(:id)
#ascending order
@books = Book.all.order(id: asc)
@books = Book.all.order(id: "ASC")
#If order is attached, all can be omitted.
#This time, specify in descending order
@books = Book.order(id: desc)
@books = Book.order(id: "DESC")
That's all for this time. I myself am a beginner in programming, but I hope it will be helpful to people in the same position. Also, if there are any mistakes in the content, we would appreciate it if you could point out.
Recommended Posts