Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 each do ignore nil values
    primarykey
    data
    text
    <p>I am building an html table that should include name, rating1, rating2, and rating3. rating 1-3 come from different models than name. </p> <pre><code>resources :names do resource :rat1,:rat2,:rat3 end </code></pre> <p>Inside of my html table I'd like to include the ratings from within each of these tables but I would like to automatically skip over or ignore tables that are nil. This is because :names may only have a :rat1 and not a :rat2 or :rat3. My view should look something like this.</p> <pre><code> &lt;table&gt; &lt;thead&gt;Name&lt;/thead&gt; &lt;thead&gt;Rating 1&lt;/thead&gt; &lt;thead&gt;Rating 2&lt;/thead&gt; &lt;thead&gt;Rating 3&lt;/thead&gt; &lt;% @names.each do |name| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= name.nametext %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= name.rat1.rating %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= name.rat2.rating %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= name.rat3.rating %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; </code></pre> <p>Except that if name.rat1 is nil it will either a.) replace the value with N/A OR b.) it will leave this field blank and move on to the next. </p> <p>What is the cleanest way to do this?</p> <p>::UPDATE::</p> <p>So my issue is that the name.rat1 is nil and the name.rat1.rating is an undefined method of a nil class so both of these options will throw the same undefined method of a nil class error regardless of the || or helper method. At least thats what my current tests are showing. Any other options? or different workarounds? I'd like to avoid having to put a validation loop like this for every rat1-3 </p> <pre><code>&lt;% unless name.rat1.nil? %&gt; &lt;%= name.rat1.rating %&gt; &lt;% end %&gt; </code></pre> <p>There has to be a simpler way. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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