--Those who are making apps with Rails but want to sort some data
Model.order(Column: :DESC)
Model.order("Column DESC")
By default, it is ASC (ascending order), so it can be DESC (descending order).
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.
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