mercredi 6 juillet 2022

How can I display values from relationship on column array?

I have 2 tables (users and meetings).

I'm trying to displaying the name of the user on table index view

users
 |id|   |name|
  1      DEMO 1
  2      DEMO 2
  3      DEMO 3

meetings
 |id|    |user_id|
  1      ["1", "2"]
  2      ["2"]
  3      ["2", "3"]

The Controller /app/controllers/meetings_controller.erb

def index
  @meetings = Meeting.all
end

The View /app/views/meetings/index.html.erb

<table>
 <thead>
   <tr>
      <td>id</td>
      <td>User Names</td>
   </tr>
 </thead> 
 <tbody>
   <% @meetings.each do |meeting| %>
   <tr>
      <td><%= meeting.id %></td>
      <td><%= meeting.user_id %></td>
   </tr>
   <% end %>
 </tbody>
</table>

I'm trying to display the user_id on array relationship and i tried this code:

I got the following error using the following code

 undefined method `each' for "[\"1\", \"2\"]":String

 <% meeting.user_id do |array|%>
   <%= array.user.name %>
 <% end %>

I got the following error using the following code

 undefined method `each' for "[\"1\", \"2\"]":String

 <% meeting.user_id do |array|%>
   <%= array %>
 <% end %>

I cannot display the value relationship because of column array.

Can you please help me with this issue?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire