class Incoming < ActiveRecord::Base belongs_to :asset belongs_to :category has_one :balance, :dependent => :delete # Returns base_amount / 100.0. def amount base_amount / 100.0 end # Checks that the specified amount is numeric and sets base_amount to the # specified amount * 100 if it is. def amount=(new_amount) Float(new_amount) # Raises ArgumentError if not numeric. self.base_amount = (new_amount.to_f * 100).round end protected # Validates that base_amount is not nil and greater than 0. def validate errors.add(:amount, 'can\'t be zero.') if base_amount.nil? || base_amount < 0.01 end end