[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once

Premise

Suppose you have a table like this.

hoges table

id text content_id
1 morning 1
2 afternoon 2
3 evening 2
4 night 3
5 midnight 3
6 late night 3

Way of thinking

To retrieve the column information of multiple records associated with a specific id at once, you can display multiple data information by using the each method in the form of "instance variable.each" in the view.

How to write

  1. To use each method, you must pass an array. By using the where method, the objects that match the conditions are fetched as an array. In this case, specify Hoge (hoges table) and use the where method to assign to @hoges the id that matches the content_id carried by params.

hoges_controller.rb


  def show
    @hoges = Hoge.where(content_id: params[:id])
  end
  1. As mentioned in the concept, multiple information can be displayed by using the each method. For example, the description is as follows.

ruby:show.html.erb


  <% @hoges.each do |hoge| %>
    <%= hoge.text %>
  <% end %>

Display image

If you write the above, you will see the following display.

If content_id "1" is included in params

スクリーンショット 2020-12-24 22.57.58.png

If content_id "2" is included in params

スクリーンショット 2020-12-24 22.59.51.png

If content_id "3" is included in params

スクリーンショット 2020-12-24 23.10.06.png

Reference article

https://qiita.com/tanaka7014/items/f1c4b6505a2c672ea1f7

Recommended Posts

[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby on Rails] How to display error messages
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
(Ruby on Rails6) How to create models and tables
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby On Rails / HTML] How to display the cursor (caret) at an appropriate place when displaying a web page
(Ruby on Rails6) Display of the database that got the id of the database
[Ruby] How to retrieve the contents of a double hash
Apply CSS to a specific View in Ruby on Rails
[Ruby On Rails] How to search and save the data of the parent table from the child table
[rails] How to display db information
How to use Ruby on Rails
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to solve the local environment construction of Ruby on Rails (MAC)!
[Rails] How to create a table, add a column, and change the column type
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Ruby On Rails] How to search the contents of params using include?
[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] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Spring sample code included] How to create a form and how to get multiple records
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to add / remove Ruby on Rails columns
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] Individual display of error messages
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
How to create a header or footer once and use it on another page
[ruby] How to assign a value to a hash by referring to the value and key of another hash
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to change the column name of the table
[Ruby On Rails] How to reset DB in Heroku
[Rails] How to load JavaScript in a specific view
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
[Rails] Put the same restrictions on multiple pieces of information
[Ruby on Rails] Add a column with a foreign key constraint
[Ruby] Find a permutation consisting of 0 to 9 at high speed.
How to input multiple images at once using rake task
From Ruby on Rails error message display to Japanese localization
A note about the seed function of Ruby on Rails
[Rails] How to display the list of posts by category
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
How to run React and Rails on the same server
How to deploy a Rails application on AWS (article summary)
[Rails] How to display information stored in the database in view
How to deal with different versions of rbenv and Ruby
[Ruby on Rails] Cookie is used and displayed only once at the first access (using jquery.cookie.js)
Create a large number of records with one command using the Ruby on Rails seeds.rb file
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]