View the Presentation
1 2 3 4 5 6 7
class Rating < ActiveRecord::Base after_commit :enqueue_worker def enqueue_worker Resque.enqueue(CalculateAverageWorker, self.id) end end
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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 ResqueBus.publish("rating_created", self.attributes) end end
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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
Publishing all all data changes, especially state changes.
Examples
Enabling…
Questions?