[RUBY] How to get information about associated tables in many-to-many tables

What you can see in this article

Organize an association so that there is a many-to-many relationship How to get information on the associated table.

Premise

Language: Ruby    Take the app I'm working on as an example. I made a many-to-many association between the CD and the songs on it. Many-to-many reason: The CD contains multiple songs. The song may be on multiple CDs.

visualMusic-Page-2.png

visualMusic-Page-3.png

So Create a one-to-many table. An intermediate table is also created at the same time.

What you want to implement

Based on this discs table Viwe (show.haml.html) is prepared because I want to make a detailed page of each CD.

(URL▶︎localhost:3000/discs/1)
Example ①: When disc_id is 1
Information you want to get / display: Songs associated with CD-A (song-A, song-B, song-C)


Example ②: When disc_id is 2
Information you want to get / display: Songs linked to CD-B (song-A, song-D, song-E)     : point_right_tone1: I want to get information from related tables as well! That is the goal this time.

Example ① Table diagram

visualMusic-Page-4 (2).png If the related pink song title is also displayed on the same page, it is successful. Did you understand what you wanted to do?

Actual code

Here, we will get the id displayed in the URL. For example, disc_id: 1. The user can instruct that he wants the information on the id: 1 CD.

discs_controller.rb


class DiscsController < ApplicationController
#abridgement

  def show
    @disc = Disc.find(params[:id])
  end

#abridgement
end

ruby:show.html.haml(show.html.For erb, convert haml to html. )



- @disc.songs.each do |song|
    = song.name

For routing, I put: show like "resources: discs, only: [: show]".

point

I think the point is the description when straddling the table.

ruby:show.html.haml(show.html.For erb, convert haml to html. )



- (params[:id]Variable).(Related table name you want to earn).each do |(Variables used below)|
    =(Variables specified above).(The column name you want to display in the income table)

I thought the explanation was crap, but I wrote it in detail.

I think I was able to display it above. Thank you for your hard work!

A word Memo

I'm honestly really a beginner Currently, the merits of forming an association, Understanding of the merits of having a parent-child relationship has not caught up. However, this time, I found that by making many-to-many, other table information can be fetched even in the view linked to a single table. I noticed that the information from the URL that can be received from the client comes from "/ discs / 1", "/ discs / 1 / song", "discs / new", and so on. Also, in routing, I just noticed that I received instructions from the main information source table, received the displayed viwe information, and could describe the information fetched from the table with the controller.

If someone who is similarly worried about the basics can get some hints with this, It should make sense for me to stumble here! I thought I recorded it here as an article for myself who will stumble in the same place again in the future.       Thank you for reading to the end: relaxed: See you again: wave:

Recommended Posts

How to get information about associated tables in many-to-many tables
How to get parameters in Spark
How to get date data in Ruby
How to get the date in java
[Rails] How to get the user information currently logged in with devise
[Rails] Use devise to get information about the currently logged in user
How to get keycloak credentials in interceptor class
How to get Class from Element in Java
Library "OSHI" to get system information in Java
How to get the class name / method name running in Java
How to get values in real time with TextWatcher (Android)
How to get the longest information from Twitter as of 12/12/2016
How to monitor application information in real time using JConsole
[Rails] How to display information stored in the database in view
How to use Lombok in Spring
How to find May'n in XPath
How to use Java HttpClient (Get)
How to hide scrollbars in WebView
How to get started with slim
[rails] How to display db information
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to master programming in 3 months
[rails] How to display parent information in child view in nested relationships
How to learn JAVA in 7 days
How to install Bootstrap in Ruby
How to use InjectorHolder in OpenAM
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to get boolean value with jQuery in rails simple form
How to introduce jQuery in Rails 6
How to use classes in Java?
How to store the information entered in textarea in a variable in the method
How to name variables in Java
How to get along with Rails
How to get and add data from Firebase Firestore in Ruby
How to set Lombok in Eclipse
How to think about class design (division) in a business system (1)
How to get the length of an audio file in java
How to get JDK 11 on your mac in a comfortable way
How to concatenate strings in java
How to install Swiper in Rails
How to get the current date as a string in yyyyMMdd format
How to get the value after "_" in Windows batch like Java -version
How to get the setting value (property value) from the database in Spring Framework
How to get the absolute path of a directory running in Java
How to create your own annotation in Java and get the value
How to get Excel sheet name list in Java (POI vs SAX)
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement date calculation in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to use custom helpers in rails
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
[Note] How to get started with Rspec
How to add jar file in ScalaIDE