[RAILS] [Ruby] I want to display posted items in order of newest date

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

[Ruby] I want to display posted items in order of newest date
[Ruby] I want to reverse the order of the hash table
I want to change the value of Attribute in Selenium of Ruby
I want to use arrow notation in Ruby
I want to get the value in Ruby
I want to display the name of the poster of the comment
[Rails] I want to display the link destination of link_to in a separate tab
I want to perform high-speed prime factorization in Ruby (ABC177E)
I want to create a Parquet file even in Ruby
I want to narrow down the display of docker ps
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I want to control the display of the upper management navigation bar (Control menu) in Liferay 7 / DXP
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to display the images under assets/images in the production environment
How to get date data in Ruby
I want to display background-ground-image on heroku.
I want to use @Autowired in Servlet
I tried to make a parent class of a value object in Ruby
[Rails] I want to send data of different models in a form
I want to display the number of orders for today using datetime.
I want to display images with REST Controller of Java and Spring!
I want to select multiple items with a custom layout in Dialog
[Ruby] I want to extract only the value of the hash and only the key
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I want to display a PDF in Chinese (Korean) with thin reports
I tried to make full use of the CPU core in Ruby
I want to display an error message when registering in the database
Use hashes well in Ruby to calculate the total amount of an order
I want to sort by tab delimited by ruby
I want to output the day of the week
I want to send an email in Java.
[Ruby] I want to do a method jump!
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
Development memo ~ I want to display only the first image posted multiple times ~
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I always want to show what I posted only to myself who is logged in
[Ruby] I want to make a program that displays today's day of the week!
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
I want to use Clojure's convenient functions in Kotlin
I want to call a method of another class
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to know the answer of the rock-paper-scissors app
I want to define a function in Rails Console
I want to stop snake case in table definition
I want to click a GoogleMap pin in RSpec
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
Handling of date and time in Ruby. Use Date and Time properly.
[Java] I want to write asynchronous processing using Promise in Java-Trial of Promise-like grammar of JavaScript-
I want to recreate the contents of assets from scratch in the environment built with capistrano
I want to find a relative path in a situation using Path
I want to set the conditions to be displayed in collection_check_boxes
I want to expand the clickable part of the link_to method