[Order method] Set the order of data in Rails

Reference target person

--Those who are making apps with Rails but want to sort some data

When you want to sort the data under certain circumstances

Uninflected word

Model.order(Column: :DESC)
Model.order("Column DESC")

By default, it is ASC (ascending order), so it can be DESC (descending order).

Actually try to get the sorted data group

feed_controller.rb


#Get all the data in the database
feeds = Feed.order(id: :DESC)

#Get all data of currently logged in user
feeds = current_user.feeds.order(id: :DESC)

This time, I tried to get the data called Feed model. Above, get all the data in the database, Below is all the data of the currently logged in user.

By doing so, it is possible to acquire a data group in descending order.

When you want to change the default settings and sort

Set the order in the model file

models/feed.rb


default_scope -> { order(created_at: :desc) }

This time as well, I set the data sorting setting called Feed model. This notation is called a lambda function and is special.

By doing this, you can set the default setting to sort in descending order of the created date and time (created_at).

Recommended Posts

[Order method] Set the order of data in Rails
Explanation of the order of rails routes
Order of processing in the program
The identity of params [: id] in rails
I tried to sort the data in descending order, ascending order / Rails
[Java] Handling of JavaBeans in the method chain
The order of Java method modifiers is fixed
[Rails] Regarding the presence or absence of parentheses in the argument of the render method
[Rails] Ranking and pagination in order of likes
How to make a unique combination of data in the rails intermediate table
How to set the display time to Japan time in Rails
[Ruby / Rails] Set a unique (unique) value in the class
Rails6: Input the initial data of ActionText using seed
Set the maximum number of characters in UITextField in RxSwift
SSL in the local environment of Docker / Rails / puma
Arrange posts in order of likes on Rails (ranking)
about the where method (rails)
[Enum] Let's improve the readability of data by using rails enum
The story of throwing BLOB data from EXCEL in DBUnit
Rails sorting function implementation (displayed in order of number of like)
Set the time zone in the JVM of your Azure application
[Rails] Where to be careful in the description of validation
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
Order of route evaluation in Spark
[Rails] Check the contents of the object
About the symbol <%%> in Rails erb
Check the migration status of rails
Filter the fluctuations of raw data
About the role of the initialize method
[Rails 6] destroy using the resources method
Data is not registered in Rails.
Call the super method in Java
[Java] Get the dates of the past Monday and Sunday in order
Fix the file name of war to the one set in Maven
[Rails] How to omit the display of the character string of the link_to method
How to delete large amounts of data in Rails and concerns
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Spring validation was important in the order of Form and BindingResult
Replace preview by uploading by clicking the image in file_field of Rails
When log data accumulates in Rails and the environment stops working
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Ruby on Rails] Introduction of initial data
Get the result of POST in Java
[Rails] I want to send data of different models in a form
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
[Rails] Temporary retention of data by session
The problem that the contents of params are completely displayed in the [Rails] view
Code of the part where server.servlet.session.timeout is set in spring.session.timeout in spring-boot2 system
[Rails] How to use the map method
[Rails / simple_format] Helper method that reflects line breaks entered in the form
part of the syntax of ruby ​​on rails
[rails] List of actions defined in Controller
Set the number of seconds for fast forward and rewind in ExoPlayer
Rails refactoring story learned in the field
The story of AppClip support in Anyca
How to change the maximum and maximum number of POST data in Spark
[Rails] Change the label name of f.label
What is the main method in Java?
What is the data structure of ActionText?
Steps to set a favicon in Rails
About the continuous division method learned in the 4th grade of elementary school