[RUBY] How to arrange articles (ascending / descending)

Self-introduction

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!

Please arrange the posted articles in ascending order of ID.

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.

  1. When neither ascending order nor descending order is specified. The default value is in descending order.
#Default notation
@books = Book.all 
@books = Book.all.order(:id)
  1. When specifying ascending or descending order. As I posted in the comment of the posted image, all in the foreground can be omitted when describing the order.
#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")

the end

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

How to arrange articles (ascending / descending)
How to sort in ascending / descending order with SQLite
How to deploy
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to use Map
How to write Rails
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to adapt Bootstrap
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use TreeSet
How to write dockerfile
How to uninstall Rails
How to install docker-machine
[How to use label]
How to make shaded-jar
How to write docker-compose
How to use hashes
How to create docker-compose
How to use JUnit 5
How to install MySQL
How to write migrationfile
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to install ngrok
How to type backslash \
How to concatenate strings
[Xcode] How to arrange Xcode and Simulator screens in full screen
How to delete BOM (UTF-8)
How to create an application
How to resolve Sprockets :: DoubleLinkError
[rails] How to post images
[AWS] How to check logs
How to resolve Git conflicts
How to use Chain API
How to lower java version
[Java] How to use Map
How to use Priority Queuing
How to write good code
Java --How to make JTable
How to handle uploaded images
How to add ActionText function
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
Bit Tetris (how to write)
[Rails] How to install devise
How to deploy on heroku
How to minimize Java images