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

  • SEARCH
  • Home
  • 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 8095545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:10:02+00:00 2026-06-05T21:10:02+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.

Here are simplified versions of my models:

class Posting < ActiveRecord::Base
  belongs_to :account
  belongs_to :journal_entry

  attr_accessible :account_id, :amount
end

class JournalEntry < ActiveRecord::Base
  has_many :postings, :dependent => :destroy

  attr_accessible :narrative, :posting_date, :postings_attributes

  accepts_nested_attributes_for :postings, :allow_destroy => :true,
    :reject_if => proc { |attrs| attrs.all? { |k, v| k == '_destroy' or v.blank? } } 
end

I’ve successfully created a nested form that allows a journal entry and its list of postings to be edited together. However, most journal entries will be simple and have one credit entry and one debit entry. To make the data entry easier in this case, I want to have another form that allows the user just to specify the credit account, debit account and amount.

From my research, can see 2 ways to do this:

  1. Single table inheritance where SimpleJournalEntry (which extends JournalEntry)
  2. Using ActiveModel to make a SimpleJournalEntry model that isn’t directly attached to the database, and handle the database changes myself in the controller

The SimpleJournalEntry model would have the credit account, debit account and amount and would be used for the editing of simple records. The existing JournalEntry model would still exist to allow editing of more complex records.

Which is the ‘rails way’ to handle this kind of thing? Is there another option that I’ve not considered?

  • 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-05T21:10:04+00:00Added an answer on June 5, 2026 at 9:10 pm

    I have to disagree with railsdog on this one. I think you do have a good STI candidate. In particular, I think “let [JournalEntry] handle the details of the simple or complicated save” demonstrates that you are dealing with two separate concerns.

    If your simple and complex JournalEntries have different business logic, they should be represented by two different classes. It is not hard to imagine, for example, that the complex JournalEntry might want to add validations that the simple JournalEntry does not. Or perhaps the simple JournalEntry should have fewer attr_accessible declarations than the complex JournalEntry. I would suggest:

    class JournalEntry < ActiveRecord::Base
      # shared accessors and validations
    end
    
    class SimpleJournalEntry < JournalEntry
      # simple accessors and validations 
    end
    
    class ComplexJournalEntry < JournalEntry
      # more complex accessors and validations
    end
    

    I would handle these two classes with separate controllers and views.

    On an unrelated note, the :all_blank option for #accepts_nested_attributes_for should do what your Proc does, no? From the docs: “Passing :all_blank instead of a Proc will create a proc that will reject a record where all the attributes are blank excluding any value for _destroy.” That is, accepts_nested_attributes_for :postings, :reject_if => :all_blank

    • 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 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
My company is looking for a new web developer and we are eager to

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.