How can I cache a calculated column in rails?

前端 未结 5 1036
感动是毒
感动是毒 2020-12-14 03:06

I have a tree of active record objects, something like:

class Part < ActiveRecord::Base
  has_many :sub_parts, :class_name => \"Part\"

  def complicat         


        
5条回答
  •  独厮守ぢ
    2020-12-14 03:48

    Have a field similar to a counter cache. For example: order_items_amount and have that be a cached calculated field.

    Use a after_save filter to recalculate the field on anything that can modify that value. (Including the record itself)

    Edit: This is basically what you have now. I don't know of any cleaner solution unless you wanted to store cached calculated fields in another table.

提交回复
热议问题