mysql - How can i display a certain attribute in a certain table without displaying all for a current user RAILS 4 -


i have student , school database tied rails project have these attributes student table:

[id,fname,lname,created_at,updated_at,school_id,user_id] 

and school table has these attributes

[id,name,address,franchise_id,created_at,updated_at] 

so far trying display school name each student current user may have. when run it, show schools students of user on each iteration of loop. here index action method magic supposed happen.

  @child = student.where(user_id:current_user.id).pluck(:school_id)   @schoolname = school.where(id:@child).pluck(:name) 

and index page in view:

 <div class="container">  <h1><font color="white"><b>my students</font></b></h1>   <table class="table table-striped">  <thead>   <tr>   <th><font color="white"><b>id</font></b></th>   <th><font color="white"><b>fname</font></b></th>   <th><font color="white"><b>lname</font></b></th>   <th><font color="white"><b>school</font></b></th>   <th><font color="white"><b>manage</font></b></th>   <th colspan="3"></th>    </tr>   </thead>    <tbody>    <% @students.each |student| %>       <tr>         <td><font color="white"><b><%= student.id %></font></b></td>        <td><font color="white"><b><%= student.fname %></font></b></td>        <td><font color="white"><b><%= student.lname %></font></b></td>        <td><font color="white"><b><%= @schoolname %></font></b></td>        <td><%= link_to 'show', student %>        <%= link_to 'edit', edit_student_path(student) %>        <%= link_to 'destroy', student, method: :delete, data: { confirm:         'are sure?' } %></td>        </tr>         <% end %>         </tbody>        </table>         <br>         <b><%= link_to 'add student', add_students_path(@student),        {:style=>'color:#ffffff;'} %></b><br/>         <b> <%= link_to 'new student', new_student_path,          {:style=>'color:#ffffff;'} %></b>           </div> 

my model empty

instead of @schoolname in loop in view, write student.school.name if one-to-many relationship works, should work.


Comments

Popular posts from this blog

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

java - Incorrect order of records in M-M relationship in hibernate -