Skip to content

Instantly share code, notes, and snippets.

@bleonard
Created December 24, 2016 18:13
Show Gist options
  • Save bleonard/9d2fabd43ad58e62246212da727581cf to your computer and use it in GitHub Desktop.
Save bleonard/9d2fabd43ad58e62246212da727581cf to your computer and use it in GitHub Desktop.
# initializer
ResqueBus.dispatch("profile") do
subscribe "rating_created" do |attributes|
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
ResqueBus.publish("rating_created", self.attributes)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment