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 576441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:02:51+00:00 2026-05-13T14:02:51+00:00

I have an invoicing system that manages debits and credits. Basically the invoice amount

  • 0

I have an invoicing system that manages debits and credits. Basically the invoice amount is obtained by the sum of its debits and the balance is derived by taking the sum of its credits and subtracting it against the total amount.

I’m doing this with four models.

  1. Invoice
  2. Line Item
  3. Debit
  4. Credit

The way it works is via a join model (Line Item) that has a polymorphic association called recordable. Everything appears to work properly at first glance. However, inspecting the line item shows that while the recordable_id shows up alright, the recordable_type is nil.

Here is a break down of the code:

class Invoice < ActiveRecord::Base
  has_many :line_items, :dependent => :destroy
  has_many :debits, :through => :line_items, :as => :recordable
  has_many :credits, :through => :line_items, :as => :recordable
end

class LineItem < ActiveRecord::Base
  belongs_to :invoice
  belongs_to :recordable, :polymorphic => true
  belongs_to :credit, :class_name => "Credit", :foreign_key => "recordable_id"
  belongs_to :debit,  :class_name => "Debit",   :foreign_key => "recordable_id"
end

class Credit < ActiveRecord::Base
  has_many :line_items, :as => :recordable, :dependent => :destroy
end

class Debit < ActiveRecord::Base
  has_many :line_items, :as => :recordable, :dependent => :destroy
end

Can anyone point me into the right direction here?

  • 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-05-13T14:02:51+00:00Added an answer on May 13, 2026 at 2:02 pm

    You are declaring ambigous associations on your LineItem class.

    In a nutshell, belongs_to does this in your class:

    1. belongs_to :invoice creates a method invoice which searches the invoices table for a record referenced by invoice_id
    2. belongs_to :recordable, :polymorphic => true creates a method recordable which searches the recordable_type.underscore.pluralize table for a record referenced by recordable_id
    3. belongs_to :credit, :class_name => "Credit", :foreign_key => "recordable_id" creates a method credit which searches through the credits table for a record referenced by recordable_id. Note, that recordable_type is ignored here.
    4. same applies to belongs_to :debit respectively.

    Since your LineItem may only belong to either a Credit or a Debit it makes no sense to declare these associations additionally. You can refer to these via the recordable association.

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

Sidebar

Related Questions

G'day guys, trying to build a small invoicing system (that can generate PDF's using
I have a booking system that i'm trying to set up. I want to
I have an Erlang system that runs user specified programs on many machines. It
We currently have an automated system that runs as a service for processing satellite
I have written a program that invokes a system command from inside: #include <stdlib.h>
VB.NET 2010, .NET 4 Hello all, I have a System.Timers.Timer object that does some
I have a project involving a web voting system. The current values and related
I have task involving reading SAS .xpt files using .NET. For that I'm using
Let's say I have a solution involving an iPhone app that generates some information
I have some simple constraints involving multiplication of reals in z3 that are producing

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.