Skip to content

Instantly share code, notes, and snippets.

@agungsetiawan
Last active March 19, 2020 02:13
Show Gist options
  • Save agungsetiawan/78f3c8842923a40d65edeb946eaf8d06 to your computer and use it in GitHub Desktop.
Save agungsetiawan/78f3c8842923a40d65edeb946eaf8d06 to your computer and use it in GitHub Desktop.
class AssetType < ApplicationRecord
has_many :asset_inventories
after_save :set_accuracy
def set_accuracy
update_column(:accuracy, asset_accuracy)
end
def asset_accuracy
if quantity_on_hand.to_f > 0
((asset_inventories.count.to_f / quantity_on_hand.to_f) * 100).round(2)
elsif asset_inventories.count > 0
asset_inventories.count * 100
elsif quantity_on_hand.to_f == 0 && asset_inventories.count == 0
100
else
0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment