Consequent attribute calculations with a queuing system

Posted by vrinek on Stack Overflow See other posts from Stack Overflow or by vrinek
Published on 2012-03-12T08:49:33Z Indexed on 2012/03/24 5:29 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

For all of the following assume these:

  • rails v3.0
  • ruby v1.9
  • resque

We have 3 models:

  • Product belongs_to :sku, belongs_to :category
  • Sku has_many :products, belongs_to :category
  • Category has_many :products, has_many :skus

When we update the product (let's say we disable it) we need to have some things happen to the relevant sku and category. The same is true for when a sku is updated.

The proper way of achieving this is have an after_save on each model that triggers the other models' update events.

example:

products.each(&:disable!)
# after_save triggers self.sku.products_updated
#   and self.category.products_updated (self is product)

Now if we have 5000 products we are in for a treat. The same category might get updated hundreds of times and hog the database while doing so.

We also have a nice queueing system, so the more realisting way of updating products would be products.each(&:queue_disable!) which would simply toss 5000 new tasks to the working queue. The problem of 5000 category updates still exists though.

Is there a way to avoid all those updates on the db?

How can we concatenate all the category.products_updated for each category in the queue?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails-3