Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get average of dimensions rating ? #137

Open
Ochoibekov opened this issue Dec 17, 2016 · 4 comments
Open

how to get average of dimensions rating ? #137

Ochoibekov opened this issue Dec 17, 2016 · 4 comments

Comments

@Ochoibekov
Copy link

       <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
Copy link

richill commented Jan 2, 2017

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
Copy link

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
Copy link

delblues commented Apr 7, 2017

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants