wazery / ratyrate

:star: A Ruby Gem that wraps the functionality of jQuery Raty library, and provides optional IMDB style rating.
http://ratingmoviestore.herokuapp.com
237 stars 120 forks source link

how to get average of dimensions rating ? #137

Open Ochoibekov opened 7 years ago

Ochoibekov commented 7 years ago
       <li>speed : <%= rating_for @car, 'speed', star: 5 %></li>
        <li>engine: <%= rating_for @car, 'engine', star: 5 %></li>
        <li>price  : <%= rating_for @car, 'price', star: 5 %></li>

code above shows the stars with rating, but i want to get the average of this tree displayed?

richill commented 7 years ago

i have found a temporary way around this:

in your car.rb file place the below method:

class Car < ApplicationRecord

  ratyrate_rateable "style", "atmosphere", "original_score"

  def overall_ratings(car)
    array = Rate.where(rateable_id: id, rateable_type: 'Car')
    stars = array.map {|car| car.stars }
    star_count = stars.count
    stars_total = stars.inject(0){|sum,x| sum + x }
    score = stars_total / (star_count.nonzero? || 1)
  end

  def avg_rating_dimension_atmosphere(car)
    array = Rate.where(rateable_id: id, rateable_type: 'Car').where(dimension: "atmosphere")
    stars = array.map {|car| car.stars }
    star_count = stars.count
    stars_total = stars.inject(0){|sum,x| sum + x }
    score = stars_total / (star_count.nonzero? || 1)
  end

  def avg_rating_dimension_style(car)
    array = Rate.where(rateable_id: id, rateable_type: 'Car').where(dimension: "style")
    stars = array.map {|car| car.stars }
    star_count = stars.count
    stars_total = stars.inject(0){|sum,x| sum + x }
    score = stars_total / (star_count.nonzero? || 1)
  end
end

in your views/cars/index.html place the below code

<table>
  <thead>
    <tr>
      <th>Model</th>
      <th>Rate</th>
      <th>Average Ratings Style</th>
      <th>Average Ratings Atmosphere</th>
    </tr>
  </thead>

  <tbody>
    <% @cars.each do |car| %>
      <tr>
        <td><%= car.type %></td>
        <td>
           <ul>
              <li>Style  : <%= rating_for car, 'style', cancel_place: "right", disable_after_rate: true %></li>
              <li>Atmosphere  : <%= rating_for car, 'atmosphere', cancel_place: "right", disable_after_rate: true %></li>
           </ul>
        </td>
        <td><%= number_with_precision(car.avg_rating_dimension_style(car), precision: 1) %></td>
        <td><%= number_with_precision(car.avg_rating_dimension_atmosphere(car), precision: 1) %></td>
      </tr>
    <% end %>
  </tbody>
</table>

in your views/cars/show.html.erb

<div class="car_show">
  <p id="notice"><%= notice %></p>

  <p>
    <strong>Title:</strong>
    <%= @car.type %>
  </p>

  <p>
    <b>Original Score:</b>
    <div class="ratings_container">
      <span class="rate_img"><%= imdb_style_rating_for @car, "original_score", disable_after_rate: true, imdb_avg: true %></span>
      <span class="rate_score"><%= number_with_precision(@car.avg_rating_dimension_style(@car), precision: 1) %></span>
    </div>
  </p>

</div>

z_spefz_ref

mmplisskin commented 7 years ago

I think this is what the rating cache table is for. You could define associations as follows:

  has_many :rating_averages, class_name: :RatingCache, foreign_key: :cacheable_id do
    def with_dimension(dimension)
      where dimension: dimension
    end
  end

@car.rating_averages.with_dimension(:performance)
delblues commented 7 years ago

Hi @mmplisskin , are you having records in the tables :overall_averages and :average_caches?

I'm also can't have records of my rater (user is) at table :rates.

ifasanelli commented 4 years ago

has_many :rating_averages, class_name: :RatingCache, foreign_key: :cacheable_id do def with_dimension(dimension) where dimension: dimension end end

Can you explain me where do I insert this code?