我是一位经验丰富的 Web 开发人员,但对 Rails 很陌生。我正在编写基于复式记帐数据库的预算应用程序。数据库包含表示交易的日记帐分录,每个日记帐分录有多个过帐。每个过帐都有一个帐户和一个金额。我将借方金额表示为负金额,将贷方金额表示为正金额。
但是,我不希望用户记住正面和负面,因此我在模型上为贷方金额和借方金额制作了虚拟属性,以便用户看到单独的贷方金额和借方金额字段。
发布模型如下:
class Posting < ActiveRecord::Base
belongs_to :account
belongs_to :journal_entry
attr_accessible :account_id, :credit_amount, :debit_amount
attr_accessor :credit_amount, :debit_amount
after_validation :set_amount
after_find :split_amount
validates :credit_amount, :format => { :with => /\A(?:\d+(?:\.\d{1,2})?|(?:.\d{1,2}))?\z/ }
validates :debit_amount, :format => { :with => /\A(?:\d+(?:\.\d{1,2})?|(?:.\d{1,2}))?\z/ }
validate :check_amounts
def check_amounts
unless @account_id.blank?
if not @debit_amount.blank? and not @credit_amount.blank?
errors.add(:base, "cannot specify both credit and debit amount.")
elsif @debit_amount.blank? and @credit_amount.blank?
errors.add(:base, "must specify one of credit or debit amount.")
end
end
end
protected
def set_amount
unless @debit_amount.blank? and @credit_amount.blank?
self.amount = @debit_amount.blank? ? BigDecimal.new(@credit_amount) : -BigDecimal.new(@debit_amount)
end
end
def split_amount
@credit_amount = (self.amount.nil? or self.amount >= 0) ? self.amount : nil
@debit_amount = (self.amount.nil? or self.amount >= 0) ? nil : -self.amount
end
end
这是为 1 个模型字段(金额)使用 2 个虚拟属性(credit_amount 和 debit_amount)的正确方法吗?我尝试为直接使用基础金额字段的 credit_amount/debit_amount 编写 getter 和 setter,但这意味着我无法准确地向用户报告验证错误。