Skip to content

Instantly share code, notes, and snippets.

@leylaKapi
Created February 27, 2017 11:15
Show Gist options
  • Select an option

  • Save leylaKapi/656fb1f68e44fe786506bceef955832a to your computer and use it in GitHub Desktop.

Select an option

Save leylaKapi/656fb1f68e44fe786506bceef955832a to your computer and use it in GitHub Desktop.
trigger
after_save :update_subtotal
def calc_subtotal
order_items.includes(:model).collect {|oi| oi.valid? ? (oi.unit_price.to_f) : 0}.sum
end
def update_subtotal
self.update_column(:subtotal, calc_subtotal)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment