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 8685063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:28:22+00:00 2026-06-12T22:28:22+00:00

Second week on RoR (with no programming background). And I have a bit of

  • 0

Second week on RoR (with no programming background). And I have a bit of an issue, I’m doing a Metacritic type of a website. And there are going to be ratings everywhere. I decided on 0 to 33 = red 34 to 66 = orange 67 to 100 = green which looks like that

index (controller:show)

 <td><% if show.reviews.count == 0 %>0
    <% elsif show.reviews.average("rating").between?(33, 66) %>
    <table class="orange">
      <tr>
        <td><b><%= number_with_precision(show.reviews.average("rating"), :precision => 0) %></b></td>
     </tr>
    </table>
    <% elsif show.reviews.average("rating").between?(66, 100) %>
    <table class="green">
      <tr>
        <td><%= number_with_precision(show.reviews.average("rating"), :precision => 0) %></td>
      </tr>
    </table>
    <% elsif show.reviews.average("rating").between?(00, 33) %>
    <table class="red">
      <tr>
        <td><%= number_with_precision(show.reviews.average("rating"), :precision => 0) %></td>
      </tr>
    </table>
    <% end %>
  </td>

My issue is that I’m gonna need to repeat that code, a lot, see (I’m only getting started:
show (controller show)

<p>
  Note: <% if @ratings == 0 %>0
  <% elsif @ratings.between?(33, 66) %>
  <table class="orange">
    <tr>
      <td><b><%= number_with_precision(@ratings, :precision => 0) %></b></td>
    </tr>
  </table>
  <% elsif @ratings.between?(66, 100) %>
  <table class="green">
    <tr>
      <td><%= number_with_precision(@ratings, :precision => 0) %></td>
    </tr>
  </table>
  <% elsif @ratings.between?(00, 33) %>
  <table class="red">
    <tr>
      <td><%= number_with_precision(@ratings, :precision => 0) %></td>
    </tr>
  </table>
  <% end %>
</p>

Somebody told me this should be a model but I don’t really know how to write it. Any help ?

  • 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-12T22:28:23+00:00Added an answer on June 12, 2026 at 10:28 pm

    First of all you should add an instance method to your Show model that retrieves and caches the average rating for a show. This prevents querying the database multiple times for the same data:

    def average_rating
      @average_rating ||= self.reviews.average('rating')
    end
    

    The code that returns the appropriate css class for a Show can go into a helper (e.g. the ShowHelper):

    module ShowHelper
      def average_rating_class_for(show)
        if show.average_rating < 34
          'red'
        elsif show.average_rating > 66
          'green'
        else
          'orange'
        end
      end
    end
    

    With this, your views become much cleaner:

    <td>
    <% if show.reviews.count == 0 %>
      0
    <% else %>
      <table class="<%= average_rating_class_for(show) %>">
        <tr>
          <td><%= number_with_precision(show.average_rating, :precision => 0) %></td>
        </tr>
      </table>
    <% end %>
    </td>
    

    And:

    <p>
      Note:
    <% if @show.reviews.count == 0 %>
      0
    <% else %>
      <table class="<%= average_rating_class_for(@show) %>">
        <tr>
          <td><%= number_with_precision(@show.average_rating, :precision => 0) %></td>
        </tr>
      </table>
    <% end %>
    </p>
    

    You could even move the generation of the entire table into a model. (Although you shouldn’t be using a table here, but that’s a different matter.)

    module ShowHelper
      def average_rating_class_for(show)
        if show.average_rating < 34
          'red'
        elsif show.average_rating > 66
          'green'
        else
          'orange'
        end
      end
    
      def average_rating_table_for(show)
        if show.reviews.count == 0
          '0'
        else
          content_tag :table do
            content_tag :tr do
              contect_tag :td, :class => average_rating_class_for(show) do
                number_with_precision(show.average_rating, :precision => 0)
              end
            end
          end
        end
      end
    end
    

    With this you view becomes:

    <td>
      <%= average_rating_table_for(show) %>
    </td>
    

    What Andre suggests is possible too, but it may be a bit difficult to comprehend for a beginner like yourself. This is simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my second programming assignment in my Java class, we have to create a
I have been struggling with this issue for one week, and still far away
(second question today - must be a bad day) I have a dataframe with
Every second line is coloured on the table in the view. I have the
I have a 30 second audio file and I would like to get callbacks
It appears that second windows are not closing. Here is what I am doing....
I am taking a Coursera Functional Programming in Scala class. This is the second
ok so i have this array of about 151 elements and there is a
I just learned prolog for a week and I have to write a prolog
I have been learning SSL/TSL and certificates for a week. It looks like it

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.