Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access id in model
    text
    copied!<p>I have a user class, which has_many resumes, each of which has many items. On my users/show page, I render multiple resumes, which is working. In my users_controller I have the following:</p> <pre><code>def show ... @resumes = @user.resumes.paginate(page: params[:page]) @resume = @user.resumes.build if user_signed_in? @resume_items = @user.res.paginate(page: params[:page]) @edu_items = @resume.edu.paginate(page: params[:page]) ... end </code></pre> <p>I defined the function res in my User model:</p> <pre><code>def res Resume.where("student_id = ?", id) end </code></pre> <p>And that worked quite well. However, I'm trying to do the same thing with the function edu in my Resume model:</p> <pre><code>def edu Education.where("resume_id = ?", id) end </code></pre> <p>but it's not working, @edu_items isn't being set to anything. Now I know it has to do with this method specifically, because if I change id to the id of a specific resume, that resume's items are rendered correctly, except across every resume. I know it's a simple fix, I've just been staring at it for way too long at this point and can't figure it out. Any advice would be amazing.</p> <p>EDIT: @makaroni4: Instead of having @educations = @user.educations, I'd rather keep the items from each resume separate. Is it possible to define a method like the educations one that will make @educations = @resume.educations?</p> <p>EDIT 2: I managed to get what I was trying to do to work, thanks for the advice. I solved it by doing away with the edu method altogether, and passing local variables to the partial:</p> <pre><code> &lt;%= render :partial =&gt; 'shared/edu', :as =&gt; :educations, :locals =&gt; {:resume_educations =&gt; resume_item.educations} %&gt; </code></pre> <p>shared/edu</p> <pre><code>&lt;% if resume_educations.any? %&gt; &lt;ol class="educations"&gt; &lt;%= render partial: 'shared/edu_item', collection: resume_educations %&gt; &lt;/ol&gt; &lt;%= will_paginate @educations %&gt; &lt;% end %&gt; </code></pre> <p>Probably not the cleanest solution, but it seems to work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload