How to use Ruby on Rails

Programming study diary

June 9, 2020 Progate Lv.85 I used Rails for my lesson assignments

Sort in ascending order and descending order

Use ʻorder` to sort the data in ascending order (asc: small order) and descending order (desc: large order).

books = Book.order("id": "asc")  #Arrange ids in ascending order
books = Book.order("id": "desc")  #Arrange ids in descending order

Specify the number to get

Use limit to specify the upper limit of the value to be retrieved.

books = Book.limit(100).order("id": "desc") #Get up to 100 values

As in the example, ʻorder can be described in one line by using limit and dot (.`).

Get all numbers

Use ʻall` to get all values as opposed to limit.

books = Book.all

Get the value you want

Use where to get the values that match the conditions in the table.

books.where("author=Ryunosuke Akutagawa") #authorカラムがRyunosuke Akutagawaのレコードを全て取得。

Recommended Posts

How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] How to use session method
[Rails] How to use enum
How to use Ruby return
[Rails] How to use enum
How to use rails join
Ruby: How to use cookies
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to use devise (Note)
[Ruby on Rails] How to write enum in Japanese
[Rails] How to use flash messages
[Ruby on Rails] How to change the column name
How to deploy Bootstrap on Rails
How to use Bio-Formats on Ubuntu 20.04
[Ruby On Rails] How to reset DB in Heroku
[Ruby] How to use any? Method
Rails on Tiles (how to write)
[Rails] How to use Active Storage
(Ruby on Rails6) How to create models and tables
[Introduction to Rails] How to use render
How to use Ruby inject method
How to display a graph in Ruby on Rails (LazyHighChart)
A memorandum on how to use Eclipse
How to use custom helpers in rails
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
Ruby length, size, count How to use
How to use MySQL in Rails tutorial
Deploy to Heroku [Ruby on Rails] Beginner
[Ruby] How to use slice for beginners
Preparing to introduce jQuery to Ruby on Rails
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to use video_tag to display videos
[Ruby on Rails] Button to return to top
Ruby: CSV :: How to use Table Note
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
How to use map
How to use collection_select
How to use Twitter4J