The following error message occurred while implementing the function to acquire all the information of the table and acquire the value of another table via the intermediate table associated with the acquired value.
Corresponding source code
Where the error occurred
<% @ task.each do | g |%> <p><% = g.genre_task.genre.user_id%></p> <% end%>
Controller of the corresponding part
def index @task = Task.order (id :: DESC) .all end
class Genre</pre> <p><br /> Task model</p> <pre><code data-language = "model">class Task</pre> <p><br /> Intermediate table model</p> <pre><code data-language = "model">class GenreTask</pre> <p><br /> genres table</p> <pre><code data-language = "Ruby">class CreateGenres</pre> <p><br /> tasks table</p> <pre><code data-language = "Ruby">class CreateTasks</pre> <p><br /> Intermediate table</p> <pre><code data-language = "Ruby">class CreateGenreTasks</pre> <strong>What I tried</strong> <p>Since NoMethodError is displayed, I thought that the specified method was different and searched on the net. However, changing the method specification did not resolve the error. Therefore, I thought that there might be another cause, so I acquired the value of the intermediate table on the console and checked whether the association was established.</p> <pre><code> pry (main)>@genre_task = GenreTask.find (1) GenreTask Load (0.4ms) SELECT `genre_tasks`. * FROM` genre_tasks` WHERE `genre_tasks`.`id` = 1 LIMIT 1 =>#<GenreTask: 0x00007fd038cadae0 id: 1, genre_id: 1, task_id: 1, created_at: Wed, 18 Nov 2020 09:29:14 UTC +00: 00, updated_at: Wed, 18 Nov 2020 09:29:14 UTC +00: 00>  pry (main)>@ genre_task.genre Genre Load (0.3ms) SELECT `genres`. * FROM` genres` WHERE `genres`.`id` = 1 LIMIT 1 =>#<Genre: 0x00007fd03ea07bf0 id: 1, text: "test", name: "test", user_id: 1, created_at: Wed, 18 Nov 2020 09:29:04 UTC +00: 00, updated_at: Wed, 18 Nov 2020 09:29:04 UTC +00: 00>
Supplementary information (FW/tool version, etc.)
Therefore, it turned out that it is not impossible to obtain the value of the association.
So I think the way to call the values in the intermediate table is wrong, but I don't know what to do.
Ruby on Rails ver6.0.0
Answer # 1
has_many: genres, through :: genre_tasksSo I have a method called genre_tasks, but there is no singular.
If you want to get the user of the genre of the task multiple, you have to take the multiple.
<% g.genres.each do | genre |%> <p><% = genre.user_id%></p>
is not it.
In addition, in Task
has_many: genre_tasksIs required.
- ruby - create 3 or more columns in the intermediate table | db design
- ruby - [rails/rspec] intermediate table model test does not pass
- ruby - i created an intermediate table, but no data is saved in the intermediate table
- ruby - i want to store the ids of user and group in an intermediate table
- ruby on rails 6 - rails does not save to intermediate table
- ruby - i want to replace a specific value in a column of multiple records
- [sqlite, ruby, rails] i want to sort the hash value data obtained from the table as i expected
- ruby - unable to get initial image when using s3 with active storage
- ruby - [rails 5] i want to send an email to a specific user using action mailer
- ruby - i want to collect the same records using rails group and get the value
- ruby - the value is not assigned successfully
- [ruby on rails 6] i implemented the tag function using acts-as-taggable-on, but i can't jump to the linked article from the list
- ruby - unable to get value in nested show action
- ruby - i get a validation error when using factorybot with rspec
- ruby - i want to change the way of arranging the hash value data obtained from the table
- ruby - param is missing or the value is empty: comment
- ruby on rails - [rails 6] i'm using bootstrap 4, but the layout collapses only after logging out
- c # - no value is assigned when using if statement
- ruby on rails 5 - how to check the value of before_action with debugger
- ruby - the last db setting doesn't work when deploying rails app on aws
- ruby - i want to make a graph of the values retrieved by the each statement with chart kick
- ruby - i want to reflect css
- ruby - use form_with in rails to specify multiple models and save values
- ruby - data is not updated by update action rails
- ruby - criate action does not pass and is not saved
- ruby - schedulerb doesn't work well
- ruby - ssl_connect returned = 1 errno = 0 state = error: wrong version number
- ruby - i want to hide a specific record
- [ruby on rails] i want to implement a follow function, but the like button is not displayed in the view