3

I have a method to calculated the average for a given set of records:

input = params[:recommendation_ratings].values  # The params are sent from radio_tags in my view.

input.each do |mini_params|
rating_id = mini_params[:rating_id]
l = Rating.find(rating_id) #Find record on Rating table. 
l.rating #Get value associated with rating_id
total_rating = []
total_rating << l.rating
average = total_rating.inject{ |sum, el| sum + el }.to_f / total_rating.size
puts average
end

l.rating is not being appended to the total_rating array. The puts average is being printed as:

3.0
3.0
3.0
3.0
3.0

How do I append each of the ratings being returned to the array to calculated the average,and other math functions.

3
  • 1
    based on your code, total_rating will always have 1 element only because you're setting it to an empty array. Commented Feb 8, 2013 at 6:24
  • What do I change so that each of the ratings are added to that empty array? Thanks! Commented Feb 8, 2013 at 6:25
  • can you update the code and paste the whole method from where this code came from? Commented Feb 8, 2013 at 6:27

3 Answers 3

8

try:

total_rating = []    
input.each do |mini_params|
  rating_id = mini_params[:rating_id]
  l = Rating.find(rating_id) #Find record on Rating table. 
  total_rating << l.rating      
end
average = total_rating.sum / total_rating.size.to_f
Sign up to request clarification or add additional context in comments.

Comments

2

this should solve your issue but there is a better way

total_rating = []    
input.each do |mini_params|
  rating_id = mini_params[:rating_id]
  l = Rating.find(rating_id) #Find record on Rating table. 
  total_rating << l.rating
  average = total_rating.inject(:+).to_f / total_rating.size
  puts average
end

So given an array of ids, try the following

Rating.where(id: mini_params[:rating_id]).average(:rating)

UPDATE: fixing the one line version

rating_ids = input.map { |mini_params| mini_params[:rating_id] }
Rating.where(id: rating_ids).average(:rating)

6 Comments

This is giving me the same output as before.
Perhaps you mean Rating.where("id in (?)", input.map{ |i| i[:rating_id] }).average(:rating) ?
yup nurettin, you're right. i assumed that mini_params[:rating_id] is the array of ids.
@Yogzzz if this is giving the same output (3 for all averages), then that means the rating for the records you're finding is 3. can you check that out?
@jvnill, you're right. The update you provided works, however it is returning 3 results(because its in the block). Moving it out of the block returns what I need. Thanks!
|
0

You need to first get all of the ratings you are trying to average. I'm assuming you have already done this, and all of those ratings are stored in total_rating. Then you need to add the specific rating to that array:

rating_id = mini_params[:rating_id]
l = Rating.find(rating_id)
total_rating << l
average = total_rating.sum.to_f / total_rating.size
puts average

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.