Skip to content

Instantly share code, notes, and snippets.

@bleonard
Created December 24, 2016 18:12
Show Gist options
  • Save bleonard/1ba7e68c27d4ff83b75348c89087378c to your computer and use it in GitHub Desktop.
Save bleonard/1ba7e68c27d4ff83b75348c89087378c to your computer and use it in GitHub Desktop.
class CalculateAverageWorker
@queue = :default
def self.perform(rating_id)
rating = Rating.find(rating_id)
total = Rating.where(rabbit_id: rating.rabbit_id).count
sum = Rating.where(rabbit_id: rating.rabbit_id').sum(:value)
profile = RabbitProfile.find_by(user_id: rating.rabbit_id)
profile.ratings_total = total
profile.rating_average = sum.to_f / (5*total.to_f)
profile.save!
end
end
class Rating < ActiveRecord::Base
after_commit :enqueue_worker
def enqueue_worker
Resque.enqueue(CalculateAverageWorker, self.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment