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 | 
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.
hoges_controller.rb
  def show
    @hoges = Hoge.where(content_id: params[:id])
  end
ruby:show.html.erb
  <% @hoges.each do |hoge| %>
    <%= hoge.text %>
  <% end %>
If you write the above, you will see the following display.
If content_id "1" is included in params

If content_id "2" is included in params

If content_id "3" is included in params

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