Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:14:37+00:00 2026-06-05T19:14:37+00:00

I am an experienced web developer, but new to rails. I am writing a

  • 0

I am an experienced web developer, but new to rails. I am writing a budget application based on a double-entry accounting database. The database contains journal entries to represent transactions, and each journal entry has multiple postings. Each posting has an account and an amount. I’ve represented a debit amount as a negative amount and a credit amount as a positive amount.

However, I don’t expect the user to remember about positive and negative, so I’ve made virtual attributes for credit amount and debit amount on my model so that the user will see separate credit amount and debit amount fields.

The Posting model is below:

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

Is this the right way to use 2 virtual attributes (credit_amount and debit_amount) for the 1 model field (amount)? I tried writing getters and setters for credit_amount/debit_amount that used the underlying amount field directly, but that meant that I couldn’t accurately report validation errors to the user.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:14:38+00:00Added an answer on June 5, 2026 at 7:14 pm

    I like the way you have written it. The credit amount and debit amount validations using regex are tighter than just validating those fields to be numeric. The split amount method could use a comment above to explain what you are doing there, so that the next guy debugging at 3:00AM doesn’t have to figure it out(actually, the regex could use an explicit description comment too).

    I hope this helps.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am an experienced web developer, but new to rails. I am writing a
I am an experienced developer but I am new to web application development. Now
I'm a fairly experienced web developer, but I've never needed to work with video
I'm an iPhone developer who's new to web development. I'm experienced HTML and CSS,
Experienced .Net developer but a complete ASP MVC (and web dev in general) novice.
I’m new to web development but have coding experience (Java) and I’m trying to
I'm a .NET web developer and just bought a new Mac. I'm interested in
Heres my problem, I'm web developer and I have a web based script(PHP/CURL/HTML/JS) that
I'm a decently experienced .net developer for the desktop but this is my first
I'm not typically a web developer, but I'm a decent enough coder that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.