Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:45:18+00:00 2026-06-18T11:45:18+00:00

I am building an html table that should include name, rating1, rating2, and rating3.

  • 0

I am building an html table that should include name, rating1, rating2, and rating3. rating 1-3 come from different models than name.

resources :names do
  resource :rat1,:rat2,:rat3
end

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.

    <table>
      <thead>Name</thead>
      <thead>Rating 1</thead>
      <thead>Rating 2</thead>
      <thead>Rating 3</thead>
      <% @names.each do |name| %>
        <tr>
          <td><%= name.nametext %></td>
          <td><%= name.rat1.rating %></td>
          <td><%= name.rat2.rating %></td>
          <td><%= name.rat3.rating %></td>
        </tr>
      <% end %>
    </table>

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.

What is the cleanest way to do this?

::UPDATE::

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

<% unless name.rat1.nil? %>
  <%= name.rat1.rating %>
<% end %>

There has to be a simpler way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:45:19+00:00Added an answer on June 18, 2026 at 11:45 am

    OFFTOPIC Your table structure is wrong. It should have <thead><tr><th>Name</th><th>Rating1</th>..so on..</tr></thead>

    So, in your case you can use the condition while rendering the rating values as:

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Rating 1</th>
          <th>Rating 2</th>
          <th>Rating 3</th>
        </tr>
      </thead>
      <tbody>
      <% @names.each do |name| %>
        <tr>
          <td><%= name.nametext %></td>
          <td><%= name.rat1.rating || 'N/A' %></td>
          <td><%= name.rat2.rating || 'N/A' %></td>
          <td><%= name.rat3.rating || 'N/A' %></td>
        </tr>
      <% end %>
      </tbody>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a table with some text that is HTML, so I am using
I'm building an HTML table that could be at most 5 columns wide and
I am building an HTML table based on the results of a query that
I have a huge html table that I am building dynamically, and I want
I have an HTML table where I'm building an edit function to change a
I am building an HTML Gui builder and this involves round-tripping HTML pages from
I'm building an html page that uses backbone.js How can I override the constructor
I'm parsing out an HTML table and building an array based on the row
I have populated an html form with MySQL data from a table. I have
I have an HTML Table that looks like this: The user can update any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.