Skip to content

Instantly share code, notes, and snippets.

@leylaKapi
Created February 27, 2017 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leylaKapi/656fb1f68e44fe786506bceef955832a to your computer and use it in GitHub Desktop.
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